bind_param不会替换我准备好的语句中的?s

时间:2019-05-02 20:02:38

标签: php sql

它成功注册了用户。但是,当我在数据库中检查它时,所有值都是0。有什么问题吗?

这是功能代码:

public function insertUser($email, $firstName, $lastName, $encryptedPassword, $salt)
{
    //SQL language - command to insert data
    $sql = "INSERT INTO users (email, firstName, lastName, password, salt) VALUES (email=?, firstName=?, lastName=?, password=?, salt=?)";

    //preparing SQL for execution by checking the validity
    $statement = $this->conn->prepare($sql);

    //if error
    if (!$statement)
    {
        throw new Exception(($statement->error));
    }

    //assigning variables instead of '?', after checking the preparation and validity of the SQL command
    $statement->bind_param('sssss', $email, $firstName, $lastName, $encryptedPassword, $salt);

    //result will store the status/result of the execution of SQL command
    $result = $statement->execute();

    return $result;
}

该函数的参数在调用时已设置为正确的值,我对此进行了测试

我刚接触PHP。如果我更正功能,则不会创建新用户。它甚至不会在浏览器窗口中打印出任何内容。这是调用此代码的代码(也许可以帮助您找到解决方案):

$result = $access->insertUser($email, $firstName, $lastName, $encryptedPassword, $salt);

//result is positive
if ($result)
{
    //throw back the user details
    $return['status'] = '200';
    $return['message'] = 'Successfully registered';
    $return['email'] = $email;
    $return['firstName'] = $firstName;
    $return['lastName'] = $lastName;

    echo json_encode($return);

    $access->disconnect();
}

2 个答案:

答案 0 :(得分:0)

您的查询是错误的。

                           //columns are declared here
$sql = "INSERT INTO users (email, firstName, lastName, password, salt) VALUES (email=?, firstName=?, lastName=?, password=?, salt=?)";
                                                                              //you do not need to declare your columns again

简单地将查询更改为

$sql = "INSERT INTO users (email, firstName, lastName, password, salt) VALUES (?, ?, ?, ?, ?)";

此外,好像您是分别存储密码和密码一样,这告诉我您正在滚动自己的哈希算法,实际上并不需要。我将删除您的salt列,并使用password_hash()作为您的密码列。

答案 1 :(得分:0)

删除列=?

$sql = "INSERT INTO users (email, firstName, lastName, password, salt) VALUES (?, ?, ?, ?, ?)";

代码

column=?    

在您的值分配中被评估为返回false(0)的布尔条件