这个问题在这里已经有答案了:
在mysql中何时使用单引号、双引号和反引号(13个答案)
在mysql中将保留字用作表名或列名时出现语法错误(1个答案)
php中单引号字符串和双引号字符串的区别是什么(12个答案)
两年前关门了。
我在尝试使用php插入mysql表时遇到语法错误。我将一个post请求从node.js发送到该页面,它应该对其进行处理并将其发送到数据库,但它只是抛出一个语法错误(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)
也很抱歉只对代码进行了基本的编辑,但我不确定要编辑什么和不编辑什么,所以我尽可能地将其包含在原始代码中;)谢谢你的帮助。
1条答案
按热度按时间elcex8rz1#
原因是“load”是一个关键字。要使列使用关键字作为名称,必须将其括在“keyword”后面。总的来说,总是这样做比较好。
以下是MySQL5.6的关键字列表:https://dev.mysql.com/doc/refman/5.6/en/keywords.html