我試圖通過PHP中插入XAMPP數據到MySQL,但我得到一個錯誤:PHP/HTML:插入數據的MySQL錯誤,請看看
mysqli_free_result() expects parameter 1 to be mysqli_result.
什麼是我的錯誤。我盡力檢查錯誤是什麼,但我無法找到它。
<?php
if(isset($_POST['insert']))
{
$hostname="localhost";
$username="root";
$password="";
$databasename="students";
$id=$_POST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$age=$_POST['age'];
$country=$_POST['country'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$connect=mysqli_connect($hostname,$username,$password,$databasename);
$query="INSERT INTO students('ID','FirstName','LastName','age','Nationality','PhoneNumber','Email') Values('$id','$fname','$lname','$age','$country','$phone','$email')";
$result=mysqli_query($connect,$query);
if($result)
{
echo 'data inserted';
}
else{
echo 'data not inserted';
}
mysqli_free_result($result);
mysqli_close($connect);
}
?>
<html>
<head> insert data </head>
<body>
<form action="connect.php" method="post">
<input type="text" name="id" placeholder="ID"><br><br>
<input type="text" name="fname" placeholder="First Name"><br><br>
<input type="text" name="lname" placeholder="Last Name"><br><br>
<input type="number" name="age" placeholder="age" min="13" max="90"><br><br>
<input type="text" name="country" placeholder="Nationality"><br><br>
<input type="number" name="phone" placeholder="Phone Number"><br><br>
<input type="text" name="email" placeholder="Email"><br><br>
<input type="submit" name="insert" value="add data to database">
</form>
</html>
[小博](http://bobby-tables.com/)說***腳本是在SQL注入攻擊的風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***瞭解[prepared](http:// en .wikipedia.org/wiki/Prepared_statement)[MySQLi]的聲明(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! [不相信它?](http://stackoverflow.com/q/38297105/1011527) –
你檢查了你的錯誤日誌嗎?你假設查詢正在工作。其 –