1

I have a MySQL database that I'm working with, but when I try to update a row in it, it doesn't work. Here's the update code I'm working with:

mysql_query("UPDATE offtopic SET next = '$insert' WHERE id = '$id'"); 

3 Answers 3

5

First of all, you should make it a bit more safe:

mysql_query(sprintf("UPDATE offtopic SET next = '%s' WHERE id = '%s'", mysql_real_escape_string($insert), mysql_real_escape_string($id)); 

Now, is your id actually string, and not numeric? If its numeric, you should rather have:

mysql_query(sprintf("UPDATE offtopic SET next = '%s' WHERE id = %d", mysql_real_escape_string($insert), $id); 
Sign up to request clarification or add additional context in comments.

1 Comment

The former should work even if id is a numeric and also more safe - in case someone manages to get non-numbers into tha variable.
5

your syntax is correct, so it might be an error with the variables or your field names.

Try this:

$sql = "UPDATE offtopic SET next = '$insert' WHERE id = '$id'"; if (!mysql_query($sql)) { echo "MySQL Error: " . mysql_error() . "<br />" . $sql; } 

That might show you some useful information to help you debug.

Comments

1

Please, for the love of the internet, don't built an SQL query yourself. Use PDO.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.