Jump to content

Prepared Statement On Duplicate Help


crwork

Recommended Posts

I have a working prepared statement in PHP that does an INSERT.

 

However, I'd like to leverage ON DUPLICATE KEY to update one field in the event the record already exists.

 

I'm having trouble using the '?' in the ON DUPLICATE clause. Here's what I'd like to do, but isn't working:

 

INSERT INTO user_prefs

(user_id, user_name, filter_prefs, email)

VALUES

(?, ?, ?, ?)

ON DUPLICATE KEY

UPDATE filter_prefs = ?

 

$stmt->bind_param('isss', $user_id, $user_name, $filter_prefs, $email);

 

Without the ON DUPLICATE, this works, but PHP seems to expect another bind parameter if I use a '?' in the On DUPLICATE clause::

 

"mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement"

 

I also tried in the ON DUPLICATE clause updating all 4 fields, but I still got the bind_param() error.

 

I searched all over and couldn't find an example for this.

 

Anyone have thoughts? I could use mysqli_real_escape_string on filter_prefs, but that seems like a cluge...

Link to comment
https://forums.phpfreaks.com/topic/269074-prepared-statement-on-duplicate-help/
Share on other sites

You have 5 question marks and only 4 variables. Not sure why this is confusing. Add the variable again.

 

I realize that. I was under the assumption that I wouldn't need to supply another parameter in the bind for the ON DUPLICATE clause '?' but it looks like I do .All set now, thanks.

 

$stmt->bind_param('issss', $user_id, $user_name, $filter_prefs, $email, $filter_prefs);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.