错误:INSERT INTO预订(GameID,Name,Numberofdays,ReservationID,Startdate)VALUES(5,'jp',4 ,,'2016-03-23') 您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在第30行结果“2016-03-23”附近使用正确的语法
即使SQL查询正确,下面的代码也会插入从表单输入的信息。我已经多次测试和更改了代码,并与同行讨论并进行了审核。
Bellow是它的代码:
<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();
?>
答案 0 :(得分:0)
省略括号:
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
或其他东西。
答案 1 :(得分:0)
您不需要括号,还有一个额外的逗号:
$qry_reserve = "INSERT INTO reservations VALUES ($GameID,'$Name',$Numberofdays,'$Startdate'";