PHP 插入不能正常工作

发布于 2024-10-10 08:15:13 字数 622 浏览 0 评论 0原文

我的文件中有以下几行 PHP 代码以及其他一些代码:

$command = "INSERT INTO inventory_items (Index, Name, Price) VALUES (NULL, 'Diamond', '3.99')";
$insertion = mysql_query($command) or die(mysql_error());
if ($insertion == FALSE)
{
 echo "Error: Insert failed.";
}
else
{
 echo "Insert successful.";
}

它不断返回此错误:

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 'Index, Name, Price) VALUES (NULL, 'Diamond', '3.99')' at line 1

myAdmin 说我正在使用 MySQL 客户端版本 5.0.91。我做错了什么?我就是想不通!我尝试了很多搜索...

I have the following lines of PHP code in my file along with some other code:

$command = "INSERT INTO inventory_items (Index, Name, Price) VALUES (NULL, 'Diamond', '3.99')";
$insertion = mysql_query($command) or die(mysql_error());
if ($insertion == FALSE)
{
 echo "Error: Insert failed.";
}
else
{
 echo "Insert successful.";
}

It keeps returning this 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 'Index, Name, Price) VALUES (NULL, 'Diamond', '3.99')' at line 1

myAdmin says I am using MySQL client version 5.0.91. What am I doing wrong? I just can't figure it out! I tried searching a lot...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-10-17 08:15:13

索引是 MySQL 中的保留字,因此,您需要更改列的名称,或者使用反引号对其进行转义。试试这个$command

$command = "INSERT INTO inventory_items (`Index`, Name, Price) VALUES (NULL, 'Diamond', '3.99')";

在这里阅读有关保留字的更多信息:http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

Index is a reserved word in MySQL and as such, you need to either change the name of the column, or escape it with backticks. Try this $command:

$command = "INSERT INTO inventory_items (`Index`, Name, Price) VALUES (NULL, 'Diamond', '3.99')";

Read more about reserved words here: http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

日久见人心 2024-10-17 08:15:13

试试这个:

$command = "INSERT INTO inventory_items (`Index`, Name, Price) VALUES (NULL, 'Diamond', '3.99');";

MySQL 保留字以及如何处理它们

Try this:

$command = "INSERT INTO inventory_items (`Index`, Name, Price) VALUES (NULL, 'Diamond', '3.99');";

MySQL reserved words and how to treat them.

兲鉂ぱ嘚淚 2024-10-17 08:15:13

您能否验证 inventory_items 表中的列是否为:

Index
Name
Price

并且您已将 Index 字段设置为 AUTO_INCRMENT。

最好的办法可能是从插入语句中删除该字段。

尝试一下,

$command = "INSERT INTO inventory_items (Name, Price) VALUES ('Diamond', '3.99')";

因为您无论如何都没有插入索引。

希望有帮助!

Can you verify that the columns in your inventory_items table are:

Index
Name
Price

And that you have the Index field set to AUTO_INCREMENT.

The best thing is probably to remove that field from your insert statement.

Try

$command = "INSERT INTO inventory_items (Name, Price) VALUES ('Diamond', '3.99')";

Since you're not inserting an Index anyway.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文