2016-11-30 45 views
0

我試圖通過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> 
+1

[小博](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) –

+1

你檢查了你的錯誤日誌嗎?你假設查詢正在工作。其 –

回答

0

問題是,您是從您的發佈代碼中引用您的列,如下所示。從其他列名刪除這些報價它視爲字符串文字而不是列

INSERT INTO students('ID','FirstName',... 
+0

其仍然顯示相同的錯誤:/ –

0
replace the last input tag with these two line 
<input type="hidden" name="insert" value="insert"> 
<input type="submit" name="submit" value="add data to database"> 

and the content.php 
<?php 
if(isset($_POST['insert'])) 
{ 
$hostname="localhost"; 
$username="root"; 
$password=""; 
$databasename="aaaaaaaaa"; 

$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 studentstbl ". 
    "(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); 
} 
?> 
+0

什麼兩行? –

+0

仍然不能正常工作 –

+0

讓我測試一下,只需幾分鐘 – alifallahi