Serhyi Vynohradov Serhyi Vynohradov - 1 year ago 123
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'";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download