我使用WAMP作为我的php服务器,我使用PHP-AdminPage作为我的数据库(在MySql中使用),一切正常,我的数据库已连接。除了我要显示数据时,创建的行都是空的!甚至我都没有把它们留空。我不明白,我很困惑。
PHP代码:
<html>
<body>
<?php
$conn = new mysqli('localhost','root','');
//check connection
if ($conn-> connect_error) {
die("Connection failed: ".$conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn,"catcafe");
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
mysqli_close($conn);
?>
</body>
</html>
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
MySQL一直告诉我错误在这里!但我不知道它是什么,这些列的名称与数据库中的名称完全相同。
我的表格/ HTML代码:
<form action="InsertReservation.php">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" required style="width:600px; font-size: 30px;">
<br>
<input type="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>
MySQL代码
SELECT * FROM `customer` ORDER BY `customer`.`FName` ASC
我希望它插入数据,但是它一直在插入空数据。
答案 0 :(得分:1)
Time
是mysql的保留字,因此请在其周围使用反引号。
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
//insert query
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,`Time`) VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $_POST['firstname'], $_POST['lastname'], $_POST['Email'], $_POST['Mobile'], $_POST['Time']);
if ($stmt->execute()) {
echo "New record created";
} else {
echo "Error, check values " . $stmt->error;
}
mysqli_close($conn);
?>
答案 1 :(得分:0)
你好,请在单个文件上使用此代码,效果很好。
谢谢
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
if($conn){
//insert query
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,Time)
VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['Email']."','".$_POST['Mobile']."','".$_POST['Time']."')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
}
mysqli_close($conn);
}
<form method="post">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" style="width:600px; font-size: 30px;">
<br>
<input type="submit" name="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>