2012-11-18 25 views
0

我正在处理这段代码,我正在使用一个简单的插入语句,我无法弄清楚为什么它不工作。如果有人能看到我做错了,请告诉我。谢谢! 这是我收到的错误:简单的插入语句的问题

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 'long,comments) 
VALUES (2 ,2012-11-18 21:25:30, 39.3436984, -76.5856958, hh)' at line 1 

这里是代码:

mysql_query ("INSERT INTO incidents (emergency_type,date_time,lat,long,comments) 
VALUES (2 ,$catchDate, $catchLat, $catchLong, $catchDescription)") or die(mysql_error()); 
echo"<br /> Data inserted"; 
+0

就是这样,感谢劳伦斯! – codenamejupiterx

回答

2

朗是一个保留字,试图`long`与反引号代替包围。

参考https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

围绕文档快速浏览发现,你应该调查PDO::preparePDO::execute做到这一点。您目前的方法似乎很容易受到SQL注入的影响。

我不是一个PHP程序员,但这样的:

$db = get a db handle from somewhere 
$st = $db->prepare('Insert Into Incidents (emergency_type, date_time, lat, `long`, comments) Values (?, ?, ?, ?, ?)'); 
$st->execute(array(2 ,$catchDate, $catchLat, $catchLong, $catchDescription)); 
+0

更好的是,如果这是您自己的表格,请将这些列名称更改为“经度”和“纬度”。 – BellevueBob

0
INSERT INTO incidents (emergency_type,date_time,lat,`long`,comments) 
VALUES (2 ,$catchDate, $catchLat, $catchLong, '$catchDescription') 

LONGMySQL Reserved Keywords名单上。改为用反向逃生。

还有一件事,date_timecomments的值必须用单引号括起来,因为它们不是数字。

和您查询是现在SQL Injection脆弱的,请花时间t阅读文章下面

+0

哦,是插入字符串而不是绑定参数?如果是这种情况,海报应该寻找不同的方式。 – Laurence

+0

@Laurence是的,但目前,提问者并没有参数化查询。 –