尝试使用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)
也很抱歉,仅对代码进行了基本的编辑,但是我不确定要编辑什么,而不要进行什么编辑,因此我将其包括为原始内容;)感谢您的帮助。
答案 0 :(得分:0)
原因是“ load”是关键字。要使您的列使用关键字作为其名称,您必须将其用反引号“关键字”括起来。通常最好总是这样做。
INSERT INTO MyTable (`something`, `load`) VALUES ("a", "b")
以下是MySQL 5.6的关键字列表: https://dev.mysql.com/doc/refman/5.6/en/keywords.html