views:

100

answers:

2

Hey everyone,

I am getting the following error and I have spent hours looking at it and cannot figure out why!

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 'primary='doej2', secondary='1' WHERE id='2'' at line 1

Here is my code:

<?php
if (isset($_POST[Edit])){

$id = $_POST['id'];
$primary = $_POST['primary'];
$secondary = $_POST['secondary'];

$query = mysql_query("UPDATE eventcal SET primary='$primary', secondary='$secondary' WHERE id='$id'");

if (!$query) {
  $_SESSION['alert'] = 'ERROR: ' . mysql_error();
}

}?>

And here is my table structure for eventcal table:

 CREATE TABLE `eventcal` (
 `id` int(10) unsigned NOT NULL auto_increment,
 `region` tinyint(3) unsigned NOT NULL,
 `primary` varchar(25) NOT NULL,
 `secondary` tinyint(1) NOT NULL,
 `eventDate` date NOT NULL,
 PRIMARY KEY  (`id`),
 KEY `primary_2` (`primary`),
 KEY `secondary` (`secondary`),
 CONSTRAINT `eventcal_ibfk_1` FOREIGN KEY (`primary`) REFERENCES `users` (`username`) ON UPDATE CASCADE
 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

Can anyone see what I'm missing? Thanks!

+5  A: 

"primary" is a reserved word in MySQL. You can put ticks around it to properly use it (as well as the other fields:

$query = mysql_query("UPDATE `eventcal` SET `primary`='$primary', `secondary`='$secondary' WHERE `id`='$id'");
Aaron W.
Adding the ticks did the trick. I would have never figured that out. Thanks!
behrk2
A: 

'primary' is a MySQL reserved word. From the documentation:

Reserved words are permitted as identifiers if you quote them as described in Section 8.2, “Schema Object Names”.

jjames