Skip to content
Advertisement

On duplicate key update – implode $row_arr

I have a script in php that writes to the mysql array:

$plu_arr = $_POST["plu"];
$il_arr = $_POST["il"];
$op_arr = $_POST["op"];
$row_index = 0;
$row_arr = array();
            
foreach($plu_arr as $plu_data){
   if (isset($il_arr[$row_index]) and !empty($il_arr[$row_index]))
   {
      $row_arr[] = "('".$plu_data."','".$il_arr[$row_index]."','".$op_arr[$row_index]."')";
   }
   $row_index++;
}
        
$ins_qry = "INSERT INTO produkty_zamowienia (plu, nr_order, il, op) VALUES ".implode(", ", $row_arr);
$db_ins = $this->__db->execute($ins_qry);

$nr_order is the same for all plu in this order. Works correctly. Adds what you need to the database. I would like to do INSERT ON DUPLICATE KEY UPDATE on the same principle

I wrote something like this:

$ins_qry = "INSERT INTO produkty_zamowienia (FK_ID_produkt, plu, nr_order, il, op) VALUES ".implode(", ", $row_arr)." ON DUPLICATE KEY UPDATE ".implode(", ", $row_arr);

But this solution writes nothing to the database and stops at $ins_qry… Please help, how can I change the existing ones and add new entries to the database … I would like $ins_qry to check if such plu + nr_order already exists than it edits $il and $op and if not, insert this row.

Advertisement

Answer

Try this:

$ins_qry = "INSERT INTO produkty_zamowienia (plu, nr_order, il, op) VALUES ".implode(", ", $row_arr)." ON DUPLICATE KEY UPDATE il = VALUES(il), op = VALUES(op)";
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement