Jump to content

INSERT and/or UPDATE using ON DUPLICATE KEY UPDATE issue


jodarecode

Recommended Posts

the code below saves but only the last field. When I update that field it updates just fine, but its confined to only that field to insert or update.

 

so, its working but for only one field and not both.

 

$fieldstoinsert=array('purchaser', 'orderno');

$query = "INSERT INTO formdata (userid, fieldid, value, saveas) VALUES";
foreach($_POST as $fieldid => $value){
    if(in_array($fieldid,$fieldstoinsert)){
        if(trim($value)!=""){
          $query.= "('$userid', '$fieldid', '$value', '$saveas'),";
        }
    }
}  
$query=substr($query,0,-1);
//Primary key is userid
$query .= " ON DUPLICATE KEY UPDATE fieldid=VALUES(fieldid), value=VALUES(value), saveas=VALUES(saveas)";  

 

echo $query;

INSERT INTO formdata (userid, fieldid, value, saveas) VALUES('27', 'purchaser', 'me', 'test1'),('27', 'orderno', '1234', 'test1') ON DUPLICATE KEY UPDATE fieldid=VALUES(fieldid), value=VALUES(value), saveas=VALUES(saveas)

no errors

 

only saves and/or updates the last field. in this case 'orderno'

Keep in mind the form has over 240 fields but I minimized it to show in forum

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.