数据无法添加到PHP中的表中

时间:2016-03-02 17:15:47

标签: php

我正在尝试获取两个输入并通过PHP将其添加到数据库中。我得到的错误是

警告:mysqli_query()需要至少2个参数,1在C:\ xampp \ htdocs \ Form中给出,第24行有DB.php

警告:mysqli_query():在第25行使用DB.php的C:\ xampp \ htdocs \ Form中的空查询

这是代码

<?php
if(isset($_GET['name']) || isset($_GET['id']))
{

$username = $_GET['name'];
$userid = $_GET['id'];
}

$host_name = 'localhost';
$user = 'root';
$password = '';
$database = 'test';

$connection = mysqli_connect($host_name,$user,$password,$database);
if ($connection) {
    echo "Connected to the DB";
    # code...
}
else
{
    echo "Not Connected";
}

$sql = mysqli_query("INSERT INTO `employee` (employee_id , Name) VALUES ($userid , $username)");
if (mysqli_query($connection,$sql)) {
    # code...
    echo "Values added to the table";
}

else
{
    echo "values not added".mysqli_error($connection);
}

?>

<form action="Form with DB.php" method="GET">
Enter Employee Name : <input type = "text" name = "name"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Enter Employee ID : <input type = "text" name = "id"><br>
<input type = "submit" name="submit" value="Submit">

1 个答案:

答案 0 :(得分:0)

你做错了,你正在调用mysqli_query两次而不是这个

$sql = "INSERT INTO `employee` (employee_id , Name) VALUES ('$userid', '$username')";
mysqli_query($connection,$sql);