在PHP中插入MySQL表时出现语法错误
我在尝试使用PHP插入MySQL表时遇到语法错误。我从node.js向该页面发送了一个POST请求,它应该对其进行处理并将其发送到数据库,但它只是抛出了一个语法错误(1064)。我是新来这里的,所以我不知道我还应该在这里放些什么,但我很乐意提供我所掌握的更多信息在PHP中插入MySQL表时出现语法错误,php,mysql,node.js,sql-insert,Php,Mysql,Node.js,Sql Insert,我在尝试使用PHP插入MySQL表时遇到语法错误。我从node.js向该页面发送了一个POST请求,它应该对其进行处理并将其发送到数据库,但它只是抛出了一个语法错误(1064)。我是新来这里的,所以我不知道我还应该在这里放些什么,但我很乐意提供我所掌握的更多信息 <head> </head> <body> <?php //Define the variables and set to empty values $steam = $load = $w
<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();
?>
只是为了将数据提交到MySQL!
不要对这个页面做任何事情!
以下是错误:
<!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)
仅用于从跟踪器向API提交数据
不要对这个页面做任何事情!
错误:插入MyTable(蒸汽、负载、重量、来源城市、目的城市、拖车损坏、收入、距离、燃油、碰撞、超速)
值(test,test,15.4,test,test,80.59,8126418,48197,20.14,5,5)
您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解在第1行“装载、重量、来源城市、目的城市、拖车损坏、收入、距离、f”附近使用的正确语法
G:\Docs\FT\tracker>node debug.js
G:\Docs\FT\tracker\debug.js:21
如果(错误)抛出错误;
^
错误:连接ETIMEDOUT
连接时。\u handleConnectTimeout(Path\node\u modules\mysql\lib\Connection.js:411:13)
在Object.onceWrapper(events.js:313:30)
在emitNone(events.js:106:13)
在Socket.emit(events.js:208:7)
在套接字上超时(net.js:422:8)
在ontimeout(timers.js:498:11)
在tryOnTimeout(timers.js:323:5)
at Timer.listOnTimeout(timers.js:290:5)
--------------------
at Protocol.\u排队(Path\node\u modules\mysql\lib\Protocol\Protocol.js:144:48)
at Protocol.handshake(Path\node\u modules\mysql\lib\Protocol\Protocol.js:51:23)
在Connection.connect(Path\node\u modules\mysql\lib\Connection.js:118:18)
at Connection.\u implyConnect(Path\node\u modules\mysql\lib\Connection.js:453:10)
在Connection.query(Path\node\u modules\mysql\lib\Connection.js:198:8)
反对。(Path\debug.js:18:5)
编译(Module.js:653:30)
在Object.Module.\u extensions..js(Module.js:664:10)
在Module.load(Module.js:566:32)
在tryModuleLoad时(module.js:506:12)
也很抱歉只对代码进行了基本的编辑,但我不确定编辑什么和不编辑什么,所以我尽可能地将其包含在原稿中;)感谢您的帮助。原因是“加载”是一个关键字。要使列使用关键字作为名称,必须将其环绕在“关键字”的反勾中。通常最好总是这样做
INSERT INTO MyTable (`something`, `load`) VALUES ("a", "b")
以下是MySQL 5.6的关键字列表:
VARCHAR列需要用引号括起来
”