Forum Moderators: coopster

Message Too Old, No Replies

Why is my query appearing to run, but not?

         

arramagong

1:56 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



Hi, I'm new to PHP/MySQL so excuse me if this is obvious but I've managed a few decent scripts so far and now am stuck.

Having successfully written a script to add a new event to a MySQL database, I'm now trying to write one to update an existing event. I have this:

$query = "UPDATE events SET
datetime = '{$datetime}',
location = '{$location}',
title = '{$title}'
WHERE id = '{$id}'
LIMIT 1";
if (mysql_query($query)) {
//Success!
echo "<p>Event successfully updated.</p>";
} else {
//Display error message
echo "<p>Event update failed.</p>";
echo "<p>" . mysql_error() . "</p>";
}

To start with I had some errors in the MySQL statement and I was getting error codes from the else statement. But now I get no errors, I get the Success statement but the database has not actually been updated. What am I doing wrong?

jatar_k

2:35 pm on Aug 6, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld arramagong,

I will rewrite the code you have, that concatenation is nasty and will cause you trouble eventually.

this would be better
$query = "UPDATE events SET datetime='$datetime', location='$location', title='$title' WHERE id=$id LIMIT 1";
mysql_query($query) or die ("<p>$query:<br>" . mysql_error());

you also might want to look at not using 'datetime' as a column name as it is also a type, try changing that

if that has the same result then echo your query

echo $query;

and copy/paste it directly into mysql to see what's going on

arramagong

3:06 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



Thanks for replying - and help with cleaner code - I'll get there eventually!

Anyway, tried echoing back $query and realised I had not defined $id. Big D'OH! I copied the variable definitions from the create event script so it didn't include id. Knew it would be something obvious. All working fine now. Thanks for the hint!

jatar_k

3:06 pm on Aug 6, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



anytime

glad you got it working