Forum Moderators: coopster
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?
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
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!