在 PHP 中插入 MySQL 表时出现语法错误 [重复]
Posted
技术标签:
【中文标题】在 PHP 中插入 MySQL 表时出现语法错误 [重复]【英文标题】:Syntax error when inserting to MySQL table in PHP [duplicate] 【发布时间】:2019-03-10 05:28:27 【问题描述】:我在尝试使用 php 插入 mysql 表时遇到语法错误。我从 node.js 向该页面发送了一个 POST 请求,它应该处理它并将其发送到数据库,但它只是抛出一个语法错误(1064)。我是新来的,所以我不知道我还应该在这里填写什么,但我很乐意提供我所拥有的更多信息。
<head>
</head>
<body>
<?php
//Define the variables and set to empty values
$steam = $load = $weight = $source_city = $destination_city = $trailer_damage = $income = $distance = $fuel = $collisions = $speeding = "";
//Set them to the values of the request
$steam = $_POST["steam"];
$load = $_POST["load"];
$weight = $_POST["weight"];
$source_city = $_POST["source_city"];
$destination_city = $_POST["destination_city"];
$trailer_damage = $_POST["trailer_damage"];
$income = $_POST["income"];
$distance = $_POST["distance"];
$fuel = $_POST["fuel"];
$collisions = $_POST["collisions"];
$speeding = $_POST["speeding"];
?>
<h2> Just for submitting the data to the MySQL!
Don't do anything with this page! </h2>
<?php
//Define the connection configuration and the datalist
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "hello";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
$sql = 'INSERT INTO MyTable (steam, load, weight, source_city, destination_city, trailer_damage, income, distance, fuel, collisions, speeding)
VALUES ($steam, $load, $weight, $source_city, $destination_city, $trailer_damage, $income, $distance, $fuel, $collisions, $speeding)';
if ($conn->query($sql) === TRUE)
echo "New record created successfully";
else
echo "Error: " . $sql . $conn->error;
$conn->close();
?>
这是错误:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2> Just for submitting the data to the API from the tracker
Don't do anything with this page! </h2>
Error: INSERT INTO MyTable (steam, load, weight, source_city, destination_city, trailer_damage, income, distance, fuel, collisions, speeding)
VALUES (test, test, 15.4, test, test, 80.59, 8126418, 48197, 20.14, 5, 5)<br>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 'load, weight, source_city, destination_city, trailer_damage, income, distance, f' at line 1
G:\Docs\FT\tracker>node debug.js
G:\Docs\FT\tracker\debug.js:21
if (err) throw err;
^
Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (Path\node_modules\mysql\lib\Connection.js:411:13)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Socket.emit (events.js:208:7)
at Socket._onTimeout (net.js:422:8)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
--------------------
at Protocol._enqueue (Path\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (Path\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (Path\node_modules\mysql\lib\Connection.js:118:18)
at Connection._implyConnect (Path\node_modules\mysql\lib\Connection.js:453:10)
at Connection.query (Path\node_modules\mysql\lib\Connection.js:198:8)
at Object.<anonymous> (Path\debug.js:18:5)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
也很抱歉只是对代码进行了基本的编辑,但我不确定要编辑什么,不应该编辑什么,所以我尽可能将其包含在原始版本中;)感谢您的帮助。
【问题讨论】:
VARCHAR 列需要用引号括起来'
【参考方案1】:
原因是“负载”是一个关键字。要让您的列使用关键字作为其名称,您必须将其括在反引号“关键字”中。通常最好总是这样做。
INSERT INTO MyTable (`something`, `load`) VALUES ("a", "b")
这里是 MySQL 5.6 的关键字列表: https://dev.mysql.com/doc/refman/5.6/en/keywords.html
【讨论】:
以上是关于在 PHP 中插入 MySQL 表时出现语法错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
插入 JSON 对象时出现 Python pymysql 语法错误
在 MYSQL 中声明 FOREIGN KEYS 时出现语法错误(使用 innoDB)
将行插入 MySQL 表时出现 pymysql.err.ProgrammingError 错误(使用 Python、Flask、ClearDB)