查询失败 您的 SQL 语法有误
Failed query You have an error in your SQL syntax
[提问]
我的 php 错误 "Failed query You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order (iduser, idfood, jumlah, total) VALUES (9, 1, 2, 20000)' at line 1"
怎么了....?
我的php
<?php
$data= file_get_contents('php://input');
$array = json_decode($data, true);
require_once ('..../db_connectfood.php');
$db = new DB_CONNECT();
foreach($array as $row)
{
$idusr = $row["iduser"];
$idfd = $row["idfood"];
$jml = $row["jumlah"];
$total = $row["total"];
$result = mysql_query("INSERT INTO order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
}
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
?>
如何从这个 php 获得价值成功和消息?
iduser
和idfood
是外键
您好,请试试这个:
$result = mysql_query("INSERT INTO databasename.order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
order 是来自 mysql 的命名词。
此致。
order
是SQL中的保留字。最好的方法是更改您的 table 的名称(例如,更改为 orders
,复数形式)。如果这不可能,您可以使用反引号来转义名称:
INSERT INTO `order` (iduser, idfood, jumlah, total)
VALUES ($idusr, $idfd, $jml, $total)
必填评论:
在 SQL 语句中使用字符串操作会使您的代码容易受到 SQL 注入攻击。您应该考虑改用 prepared statement。
order
是 reserved word.
为了能够在查询中使用保留字,您必须像`reserved word` 一样用反引号包围它来转义它(注意反引号可能看起来像单引号,但它不是)。
所以,在你的情况下改变
$result = mysql_query("INSERT INTO order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
至
$result = mysql_query("INSERT INTO `order` (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
... 并检查 table 的其余部分或列名是否为保留字。
作为最佳和安全的做法,建议使用参数化 queries/prepared 语句。
[提问] 我的 php 错误 "Failed query You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'order (iduser, idfood, jumlah, total) VALUES (9, 1, 2, 20000)' at line 1" 怎么了....?
我的php
<?php
$data= file_get_contents('php://input');
$array = json_decode($data, true);
require_once ('..../db_connectfood.php');
$db = new DB_CONNECT();
foreach($array as $row)
{
$idusr = $row["iduser"];
$idfd = $row["idfood"];
$jml = $row["jumlah"];
$total = $row["total"];
$result = mysql_query("INSERT INTO order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
}
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
?>
如何从这个 php 获得价值成功和消息?
iduser
和idfood
是外键
您好,请试试这个:
$result = mysql_query("INSERT INTO databasename.order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
order 是来自 mysql 的命名词。
此致。
order
是SQL中的保留字。最好的方法是更改您的 table 的名称(例如,更改为 orders
,复数形式)。如果这不可能,您可以使用反引号来转义名称:
INSERT INTO `order` (iduser, idfood, jumlah, total)
VALUES ($idusr, $idfd, $jml, $total)
必填评论:
在 SQL 语句中使用字符串操作会使您的代码容易受到 SQL 注入攻击。您应该考虑改用 prepared statement。
order
是 reserved word.
为了能够在查询中使用保留字,您必须像`reserved word` 一样用反引号包围它来转义它(注意反引号可能看起来像单引号,但它不是)。
所以,在你的情况下改变
$result = mysql_query("INSERT INTO order (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
至
$result = mysql_query("INSERT INTO `order` (iduser, idfood, jumlah, total) VALUES ($idusr, $idfd, $jml, $total)") or die ("Failed query ".mysql_error());
... 并检查 table 的其余部分或列名是否为保留字。
作为最佳和安全的做法,建议使用参数化 queries/prepared 语句。