MySQL 查询的语法错误 [英] Syntax Error with MySQL Query
问题描述
我收到以下错误,我花了好几个小时查看它,但不知道为什么!
I am getting the following error and I have spent hours looking at it and cannot figure out why!
错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 'primary='doej2', secondary='1' WHERE id='2'' 附近使用的正确语法
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
这是我的代码:
<?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();
}
}?>
这是我的 eventcal 表的表结构:
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!
推荐答案
"primary" 是 MySQL 中的保留字.您可以在它周围打勾以正确使用它(以及其他字段:
"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'");
这篇关于MySQL 查询的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!