Serge Vinogradov
Serge Vinogradov

Reputation: 730

Strange MySQL Error. (PHP)

I have a following code:

<?php
include("config.php");
$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?

Upvotes: 0

Views: 85

Answers (3)

Marcus
Marcus

Reputation: 12586

KEY is a reserved word in MySQL and you need to escape it using backticks to use it as a column name and also you should not use SET when inserting.

$sql = "INSERT INTO softversions (`key`) VALUES ('$key')";

Upvotes: 2

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

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'";

Upvotes: 1

Tim
Tim

Reputation: 1899

$sql = "INSERT INTO softversions(keyName) values('{$key}')";

Upvotes: 0

Related Questions