PHP/mySQL trouble...

They have: 71 posts

Joined: Aug 2001

Has any one ever had the problem that with an Update query.. it doesn't update?

I have ran the same query through mySQL and it works fine but when I come to run it through my script it doesn't update. All of the variables that go through the query are set. I have a field that resets itself to 0 when you update???!

What's goin on?

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

The best way to tell what is going on is to echo your query to the screen to see if it is passing values to the database.

They have: 453 posts

Joined: Jan 1999

and if that doesn't help, an
echo mysql_error( );

is your friend.

They have: 71 posts

Joined: Aug 2001

Okay guys this is what happens...

When I echo the variables they are fine and they show up...

There is no mysql_error(); text either, any more ideas?

the script is for an edit page on my forums that I am creating the query is like this

$query = "UPDATE topics SET tsubject='$tsubject' AND tbody='$tbody' WHERE (tid=$tid)";
$result = mysql_query($query);

if (!$result)  {

   echo "Error in query<br>\n";
   echo mysql_error();

}
'

any ideas?

They have: 447 posts

Joined: Oct 1999

are you sure $tid has a value and the record exists in the database?

He has: 1,016 posts

Joined: May 2002

a_gajic,

Try the following..

<?php
$query
= \"UPDATE topics SET tsubject='$tsubject' AND tbody='$tbody' WHERE (tid=$tid)\";
$result = mysql_query($query);

if (mysql_errno())  {
   echo \"Error in query<br>\n\";
   echo mysql_errno() . \" - \" . mysql_error();
}
?>

The reason why I suggest this is because when you run an UPDATE query, it does not return a TRUE or FALSE value in $result.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

SET doesn't use the AND field seperator, it uses the coma.

$query = "UPDATE topics SET tsubject='$tsubject', tbody='$tbody' WHERE tid='$tid'";

He has: 1,016 posts

Joined: May 2002

Mark, nice catch! Laughing out loud I didn't even bother looking at the query when I saw the !$result part.

Cheers.

They have: 71 posts

Joined: Aug 2001

damnit.. thanks Mark - I knew that Sad I hate it when that happens lol.. thanks for the info about the query not retrieving a true/false value too Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.