我試圖使用這個代碼from,當頁面被加載時,它會將空字段插入到數據庫中,然後如果表單字段被填寫並且提交查詢被按下,那麼它會向數據庫發送另一批數據(MySQL) 。在提交表單之前,Php會將數據插入到數據庫中,爲空值?
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
時頁面加載,代碼運行。代碼插入值即使爲空 - 解決方案,插入之前提交的檢查表格 – 2014-02-24 00:59:15