我整晚都在检查我的语法,但我似乎看不出哪里出了问题。我对这一切都比较陌生,并且希望能提供任何帮助。我收到的错误是
"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 ''ID','Name','Option','Sides') VALUES(NULL,'Denise','Chicken','Mashed Potat' at line 1"
我的代码如下:
<?php
include 'connect.php';
$name = $_POST['inputName'];
$opt = $_POST['inputOption'];
$side = $_POST['inputSides'];
if(!$_POST['Submit']) {
echo "Please fill out the form.";
header('Location: index.php');
} else {
mysql_query("INSERT INTO people ('ID','Name','Option','Sides')
VALUES(NULL,'$name','$opt','$side')") or die(mysql_error());
echo "User has been added.";
header('Location: index.php');
}
?>
最佳答案
您使用了错误的identifiers对于您的专栏:
('ID','Name','Option','Sides')
删除引号或用反引号括起来。
(`ID`,`Name`,`Option`,`Sides`)
另外,您当前的代码对SQL injection开放.
使用mysqli
with prepared statements ,或PDO with prepared statements ,它们更安全。
另一件事;如果您想输入撇号,请使用 stripslashes()
包括mysql_real_escape_string()
。这个机会很可能会出现。 洞察力。
- 否则,SQL 将抛出另一个错误。
关于php - 使用 PHP 将数据输入 MySQL 数据库时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26050925/