[英]MySQL throws error but works - PhpMyAdmin without error
I am running this query on my MySQL Database - with mysql_query it throws me an error but the data is still properly inserted into the table. 我在MySQL数据库上运行此查询-使用mysql_query会引发错误,但数据仍正确插入表中。 If I enter it in PhpMyAdmin it works without error. 如果我在PhpMyAdmin中输入它,则不会出现错误。
INSERT INTO `kommentare` VALUES(NULL,'1','MyName','MyEmail','MyText','2014-08-05');
PHP : PHP的:
$name = mysql_escape_string($name);
$email = mysql_escape_string($email);
$kommentar = mysql_escape_string($kommentar);
$datum = mysql_escape_string($datum);
$reiseid = str_replace("/", "", $reiseid);
$query = "INSERT INTO kommentare VALUES(NULL,'" . $reiseid . "','" . $name . "','" . $email . "','" . $kommentar . "','" . $datum . "');";
$result = mysql_query($query) or die(mysql_error());
echo $query;
Error: 错误:
You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法
How is that possible? 那怎么可能? I am experienced with MySQL but this wrecks my nerves - it works but says it doesn't?! 我对MySQL很有经验,但是这使我感到不安-它可以工作,但说不行!
UPDATE: 更新:
It just happens when I have more than one entry in the table. 当表中有多个条目时,就会发生这种情况。 ANd even if I remove all the ' it gives me the same error, saying I should check near the ' 即使我删除所有“,也会给我同样的错误,说我应该检查”
If the first column is a auto-increment primary key, you don't pass it NULL
, you pass it DEFAULT
: 如果第一列是自动增量主键,则不要将其传递为NULL
,而应将其传递为DEFAULT
:
INSERT INTO kommentare VALUES
(DEFAULT,'$reiseid','$name','$email','$kommentar','$datum');
But really you should instead be naming your columns and skipping those that you don't have a value for: 但是,实际上,您应该命名您的列,并跳过那些您没有价值的列:
INSERT INTO kommentare
(reiseid, name, email, kommentar, datum)
VALUES
('$reiseid','$name','$email','$kommentar','$datum');
SOLUTION: 解:
My id has been passed not as 1 but as 1/ for some reason. 由于某种原因,我的ID传递的不是1,而是1。 This caused MySQL to crash although it was not shown to me. 尽管没有显示给我,这导致MySQL崩溃。 I replace the / with "" now and everything works fine! 我现在用/替换/,一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.