PHP / HTML:将数据插入mysql错误请看一下

时间:2016-11-30 13:12:06

标签: php html mysql html5 xampp

我试图通过php将数据插入xampp中的mySql但是我收到错误:

  

mysqli_free_result()期望参数1为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>

2 个答案:

答案 0 :(得分:0)

问题是您从发布的代码中引用了如下所示的列。从列名中删除这些引号,否则它被视为字符串文字,而不是列

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

答案 1 :(得分: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);
}
?>