0

I have that code

    if ($res)
    {
     $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."','".$nrZamowienia."','".$il_arr[$row_index]."','".$op_arr[$row_index]."')";
       }
       if (isset($il_arr[$row_index]) and empty($il_arr[$row_index]))
       {
          $row_arr_del[] = "('".$plu_data."','".$nrZamowienia."','".$il_arr[$row_index]."','".$op_arr[$row_index]."')";
       }
       $row_index++;
    }
                
$ins_qry = "INSERT INTO table1 (plu, nr_order, il, op) VALUES ".implode(", ", $row_arr)." ON DUPLICATE KEY UPDATE ilosc = VALUES(ilosc), opis = VALUES(opis)";
$ins_qry_del = "DELETE FROM table1 WHERE plu = {$plu_data[$row_index]} AND nr_order = {$nrZamowienia}";


$db_ins = $this->__db->execute($ins_qry);
$db_ins = $this->__db->execute($ins_qry_del);

And insert and update work fine. If $il is deleted in the form, the item enters $ins_qry_dell and this is where the problem begins because the item does not remove from the table ... Will someone help me choose the right condition to remove from the table?

k_turek
  • 191
  • 9
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 16 '21 at 14:32

1 Answers1

0

I found a solution

if (empty($il_arr[$row_index]))
       {
          $row_arr_del[] = "('".$plu_data."')";
$ins_qry_del = "DELETE FROM table1 WHERE plu = ".$row_arr_del[$il_poz_del]." AND nr_order  = {$nrZamowienia}";
$db_ins = $this->__db->execute($ins_qry_del);
          if ($db_ins)
          {
            $il_poz_del++;
          }
       }
k_turek
  • 191
  • 9