MySQLi_query语法错误

时间:2016-06-16 22:44:01

标签: php mysqli

嗨,我一直试图解决这个问题几个小时......以下是代码:

function add_user($data) {
$user_table = USER_TABLE;
$first_name = ($data['first_name']);
$last_name =($data['last_name']);
$email_address = ($data['email_address']);
$drivers_license_id = ($data['drivers_license_id']);
$gender = ($data['gender']);
$password =($data['password']);
$query = "INSERT INTO $user_table (
        first_name,
        last_name,
        email_address,
        drivers_license_id,
        gender,
        password
    ) VALUES (
        '$first_name',
        '$last_name',
        '$email_address',
        '$drivers_license_id',
        '$gender',
        '$password')";
if (mysqli_query($connection,'$query',MYSQLI_USE_RESULT)) return true;
error_log("Failed to create user: " . mysqli_error($error_get_last));
return false;

我收到语法错误。我不确定是否应该将查询返回false,或者将其保留为true。    未定义的变量:C:\ xampp \ htdocs \ etc中的链接。在122号线上    注意:未定义的变量:C:\ xampp \ htdocs \ etc中的连接。在122号线上    警告:mysqli_query()最多需要3个参数,4个在C:\ xampp \ htdocs

中给出

3 个答案:

答案 0 :(得分:0)

您传递字符串“$ query”而不是传递查询本身。你的mysqli_query()应如下所示:

mysqli_query($connection,$query,MYSQLI_USE_RESULT)

答案 1 :(得分:0)

mysqli_query($connection,'$query',MYSQLI_USE_RESULT)应为mysqli_query($connection,$query,MYSQLI_USE_RESULT)

我应该提到这可能是一个非常不安全的查询。你似乎没有消毒你使用的变量,这将允许有人用sql注入攻击你。

阅读本文: https://www.troyhunt.com/everything-you-wanted-to-know-about-sql/

答案 2 :(得分:0)

试用此代码:

function add_user($data) {
    $connection = mysqli_connect('localhost','root','','database_name');
    $query = "INSERT INTO `USER_TABLE` (
        `first_name`,
        `last_name`,
        `email_address`,
        `drivers_license_id`,
        `gender`,
        `password`
    ) VALUES (
       $data['first_name'],
        $data['last_name'],
        $data['email_address'],
        $data['drivers_license_id'],
        $data['gender'],
        $data['password'])";
    if (mysqli_query($connection,'$query',MYSQLI_USE_RESULT))
         return true;
    error_log("Failed to create user: " . mysqli_error($error_get_last));
    return false;