Serhyi Vynohradov Serhyi Vynohradov - 11 months ago 80
MySQL Question

Strange MySQL Error. (PHP)

I have a following code:

$key = 'blahblah';
$sql = "INSERT INTO softversions SET key='$key'";
$result = mysql_query($sql) or die ($mysql_error());
echo "dude";

This gives me an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='svksskjfvns'' at line 1

The thing is that I've used this script about a hundred times on other pages and it worked.
Table and field names are 100% correct.

I don't understand what is going on.
Do you see the syntax error there?

Answer Source

key is a reserved word in MySQL. To use it as a column, you need to escape it every time you call it.

$sql = "INSERT INTO softversions SET `key`='$key'";