localhost中的SQL错误

时间:2016-03-13 10:24:58

标签: php

$query = "INSERT INTO 'users' (username, password, conirmpwd, gender) VALUES ('$username','$email','$password','$confirmpwd','$gender')";
    $result = mysqli_query($query);
    if($result){
        $msg="User Created Successfully";
    }else{
        $msg="Username is already taken";
    }
}

任何想法该错误有什么问题? 成功连接

  

警告:mysqli_query()需要至少2个参数,1中给出1   第12行的D:\ XAMPP \ htdocs \ codeinventor \ register.php

3 个答案:

答案 0 :(得分:1)

两件事: -

1.查看mysqli_query()手册: - http://php.net/manual/en/mysqli.query.php

mysqli_query()问两个参数,第一个是连接对象,第二个是查询。因此,请在此处提供您的连接对象,如下所示: -

 $result = mysqli_query($con,$query);// here $con is database connection object(change with yours object)

2. INSERT查询中使用的序列也不正确应该是: -

$query = "INSERT INTO `users` (username, password, conirmpwd, gender) VALUES ('$username','$password','$confirmpwd','$gender')";

或者如果您的表格中也有email列:

$query = "INSERT INTO `users` (username, password,email,conirmpwd, gender) VALUES ('$username','$password','$email','$confirmpwd','$gender')";

答案 1 :(得分:0)

您需要指定对数据库建立的连接。正如警告所说,mysqli是一种需要两个参数的方法。所以,你应该传递两个变量mysqli()as:

$con = mysqli_connect('localhost','your_db','db_username','db_pass');

$query = "INSERT INTO 'users' (username, password, conirmpwd, gender) VALUES ('$username','$email','$password','$confirmpwd','$gender')";
    $result = mysqli_query($con,$query);
    if($result){
        $msg="User Created Successfully";
    }else{
        $msg="Username is already taken";
    }
}

答案 2 :(得分:0)

您需要将mysqli资源传递给mysqli_query。像这样:

$con = mysqli_connect($host, $username, $password, $dbName);
$res = mysqli_query($con, "INSERT INTO 'users' (username, password, conirmpwd, gender) VALUES ('$username','$email','$password','$confirmpwd','$gender')");

您需要将$host$username$password$dbName替换为您的数据库信息!