Jump to content

On Duplicate Key Update


piearcy
Go to solution Solved by requinix,

Recommended Posts

Not sure why this isn't working so thought I would see if others laying eyes on it will see the problem. 

mysqli_query($mysqliConn, 'INSERT INTO '.$table.' (id, description, car_initials, car_no, site_id, product_id, status_code, available_dt)
VALUES ('.$id.', '.$description.', '.$carInitials.', '.$carNo.', '.$siteId.', '.$productId.', '.$statusCode.', '.$availableDt.')
ON DUPLICATE KEY UPDATE description = '.$description.', car_initials = '.$carInitials.', car_no = '.$carNo.', site_id = '.$siteId.', product_id = '.$productId.', status_code = '.$statusCode.', available_dt = '.$availableDt.'
') or apiError( array( 'index' => $i, 'id' => $id ) );

Description is a combination of the car initials and car number and is placed in a unique key in the MySql database. But I'm getting this:

Duplicate entry 'MWCX300310' for key 'description'. 

 

I should not be getting this since I'm doing the on duplicate key update. 

 

Am I missing something here? 

 

And yes I know this is not a prepared statement. I took this project over from another developer and am in the process of changing everything over to PDO to rectify that problem. 

 

Thanks,

Dave

Link to comment
Share on other sites

  • Solution

What keys do you have on that table? Do a SHOW CREATE TABLE $table if you're not sure.

 

More than one key? Sounds like you're violating one key (that isn't the description), triggering the ON DUPLICATE KEY, but then violating the description key with the new value.

Link to comment
Share on other sites

id is the primary key and description is a unique key. I had already looked at those. 

 

You have a point and I agree that's what's happening. Now I have to look at this further to figure out why he made description unique. 

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.