php - 在 PHP 中插入 MySQL 表时出现语法错误
问题描述
尝试使用 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)
也很抱歉只是对代码进行了基本编辑,但我不确定要编辑什么,不应该编辑什么,所以我尽可能地将其包含在原始文件中;)感谢您的帮助。
解决方案
原因是“负载”是一个关键字。要让您的列使用关键字作为其名称,您必须将其括在反引号“关键字”中。通常最好总是这样做。
INSERT INTO MyTable (`something`, `load`) VALUES ("a", "b")
这是 MySQL 5.6 的关键字列表: https ://dev.mysql.com/doc/refman/5.6/en/keywords.html
推荐阅读
- javascript - 如何等待在地图内解决的承诺?
- asp.net-mvc - 如何通过单击 ASP.Net Core MVC 中的单选按钮从模型中获取选择性数据?
- javascript - 在模态视频外点击后在后台播放
- mysql - SQL Server 存储过程到 MySQL
- php - 如何在 PHP 中加密/散列 $_GET 中的数据?
- oracle - 用于自动化的 Oracle RightNow 模拟 API
- vuejs2 - 您如何响应地切换不同显示尺寸的路线?
- python - python sqlalchemy orm 一对多双向关系 - 将json映射到多个表
- java - Swing - 在 JAR 文件中包含图像
- google-cloud-platform - Google Next 的这张幻灯片中使用了什么语言