PHP bind_params with null

2020-03-17 13:46发布

I am trying to bind params to a INSERT INTO MySQLi prepared statement if that variable exists, otherwise insert null.

This is what I have, but it is not working:

if (!empty($name)) {
    $result->bind_param('ss', $name, $url_friendly_name);
} else {
    $result->bind_param('ss', null, null);
}

if (!empty($description)) {
    $result->bind_param('s', $description);
} else {
    $result->bind_param('s', null);
}

Does anyone know of a better way of doing this or is there just a minor issue with my code. I am doing the above for each variable in the prepared statement.

2条回答
倾城 Initia
2楼-- · 2020-03-17 14:35

Keep in mind that you can't use bind_param twice, because the last statement will replace the first. Try to find a way to use bind_param just once.

查看更多
聊天终结者
3楼-- · 2020-03-17 14:51

bind_param works by reference. That means that it takes a variable, then uses the value of that variable in execute.

null is not a variable.

Try setting a variable to null and then binding that variable instead.

(Also consider using PDO instead of mysqli, where the execute method can take a simple array and you can bypass wacky binding methodology of mysqli.)

查看更多
登录 后发表回答