使用bind_param PHP在数组中动态执行查询

时间:2019-05-10 10:37:52

标签: php

我实际上开始使用prepare语句,但是我发现很有趣,但是实际上我不清楚如何在prepare语句中将数组插入bind_param中。

从下面的代码中,我使用register_user函数来抓取所有$ register_data,包括用户名,密码,电子邮件等,然后将$ register_data压入$ data和$ field的键中,但是如何绑定数组bind_param,以便我的查询可以正确动态地执行。谢谢

if (empty($_POST) == false && empty($errors) == true) {

    $register_data = array(
        'email' => $email,
        'username' => $username,
        'first_name' => $first_name,
        'last_name' => $last_name,
        'password' => $password
    );

    register_user($register_data);

    exit();

}


function register_user($register_data) {
    global $connection;

    array_walk($register_data, 'array_sanitize');
    $register_data['password'] = md5($register_data['password']);

    $fields = implode(', ', array_keys($register_data));
    $data = implode(', ', $register_data);

    $stmt = $connection -> prepare("INSERT into users ($fields) VALUES (?)");
    // the problem is around here
    $stmt -> bind_param('s', $data);
    $stmt -> execute();
}

0 个答案:

没有答案