>错误:插入到预订(游戏ID,名称,天数,预订ID,开始日期)值(5,'jp', 4, ,'2016-03-23')您的 SQL 语法有误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 30 行结果的"2016-03-23")附近使用的正确语法
下面的代码确实插入了从表单输入的信息,即使SQL查询是正确的。我已经多次测试和更改了代码,并与同行进行了讨论和审查。
波纹管是它的代码:
<div id="content">
<?php
//variables needed to connect to the database
$user_name = "root";
$password = "";
$database = "game_library";
$host_name ="localhost";
// Create connection
$con=mysqli_connect($host_name,$user_name,$password,$database) or die("Error ");
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
//link the search term to the html page
$GameID=$_POST['GameID'];
$Name=$_POST['Name'];
$Numberofdays=$_POST['Numberofdays'];
$Startdate=$_POST['Startdate'];
//sql query to add the data from the form elements to the sql database
//The reservationID is auto incremented so requires a space
$qry_reserve = "INSERT INTO reservations
(GameID,Name,Numberofdays,ReservationID,Startdate)VALUES ($GameID,'$Name',
$Numberofdays, ,'$Startdate')";
//Runs the query if the database if connection succesful
if ($con->query($qry_reserve) === TRUE) {
echo '<br/>';
echo $Name. ' has been added successfully</h2>';
echo '<hr>';
} else {
echo "Error: " . $qry_reserve . "<br>" . $con->error;
}
//show added data & all records to prove they have been added. You don't have to do this
$qry_show_table = "SELECT * FROM reservations WHERE GameID='$GameID' ";
$result = mysqli_query($con, $qry_show_table);
if (mysqli_num_rows($result) > 0) { // checks if there are more than zero rows returned.
// output data of each row
while($row = mysqli_fetch_assoc($result)) //puts all the results into an associative array that we can loop through
{
echo '<br/>';
echo 'Name: '.$row['Name'];
echo '<br/> GameID: '.$row['GameID'];
echo '<br/> Startdate: '.$row['Startdate'];
echo '<br/> Numberofdays: '.$row['Numberofdays'];
echo '<br/>';
echo '<hr>';
}
} else {
echo "0 results";
}
$con->close();
?>
省略括号:
INSERT INTO reservations
VALUES ($GameID, '$Name', $Numberofdays, ??,' $Startdate')
---------------------------------------------^ something needs to go here
或者,更好的是,列出列:
INSERT INTO reservations(col1, col2, col3, col4, col5)
VALUES ($GameID, '$Name', $Numberofdays, ??, '$Startdate')
---------------------------------------------^ something needs to go here
请注意,您有两个逗号,中间没有值。 也许这是一个错字,也许你打算NULL
或DEFAULT
或其他什么。
你不需要括号,而且你还有一个额外的逗号:
$qry_reserve = "INSERT INTO reservations VALUES ($GameID,'$Name',$Numberofdays,'$Startdate'";
更多信息请见 http://www.w3schools.com/sql/sql_insert.asp。