0

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
Serge Vinogradov
  • 710
  • 1
  • 9
  • 28
  • Think that should be: `INSERT INTO softversions (key) VALUES ('$key')` – Clive Oct 25 '11 at 17:20
  • You use SET for UPDATE statements not INSERT statements. I am very doubtful this script as you have it in the post worked anywhere else.. – Garvin Oct 25 '11 at 17:20
  • @Garvin You can actually use `INSERT INTO .. SET ..`. Here's an old question regarding that: http://stackoverflow.com/questions/861722/mysql-insert-into-table-values-vs-insert-into-table-set. – Marcus Oct 25 '11 at 17:33
  • @Marcus I will have to check this but if what you are saying is correct wouldn't his code work? – Garvin Oct 25 '11 at 17:37
  • @Garvin He still missed to escape the keyword `key` – Marcus Oct 25 '11 at 17:38
  • @Marcus I was able to confirm it..thanks – Garvin Oct 25 '11 at 17:42

3 Answers3

2

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')";
Marcus
  • 12,296
  • 5
  • 48
  • 66
1

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'";
Kaivosukeltaja
  • 15,541
  • 4
  • 40
  • 70
0

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

Tim
  • 1,840
  • 2
  • 15
  • 12