//如何修改下面的代碼,以便插入到表單中的值可以插入到我的mysql數據庫中?我可以連接到我的數據庫就好,我的數據庫被稱爲圖像和表被稱爲人。如何將表單腳本中的記錄插入到mysql數據庫中?
//This is my insert.php file
<?php
$con=mysqli_connect("localhost","root","anble","images");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$FirstName = mysqli_real_escape_string($_POST["FirstName"]);
$LastName = mysqli_real_escape_string($_POST["LastName"]);
$Age = mysqli_real_escape_string($_POST['Age']);
$sql="INSERT INTO Persons (Name, LastName, Age);
VALUES ($FirstName, $LastName, $Age)";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
// This is my form file
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input name="FirstName" type="text" value="FirstName">
Lastname: <input name="LastName" type="text" value="LastName">
Age: <input name="Age" type="text" value="Age">
<input type="submit">
</form>
</body>
</html>
// This is the error
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp \htdocs\check_php\insert.php on line 10
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\check_php\insert.php on line 11
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\check_php\insert.php on line 12
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; VALUES (, ,)' at line 1
查找'mysqli_real_escape_string()'PHP手冊,看看它期望的參數。這是非常基本的東西。 –
我今年15歲。 – user3553512
那麼?錯誤消息告訴你到底發生了什麼問題。查看手冊將爲您提供解決問題的信息。 –