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

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.