Multiple UPDATE from a dynamic field

3

I'm trying to get the% s of% s that comes from checkboxes selected by user (array), and through these id s, make an update to the table, with the following code:

$ocs_imp = $_POST['check_imprime']; 
$contar = count($ocs_imp);
$id = implode(',',$ocs_imp); 

   $inicio = 0;
   $ultimo = $contar-1;
   $arr = range($inicio, $ultimo);

$sqli = "";
     foreach ( $arr as $k => $v ) {
$sqli .= "UPDATE oc_cadastro SET imprime_oc='s', imprime_oc_data='".$pega_data."' WHERE id = '".$ocs_imp[$v]."')))";
                                  } 

                            $mysqli->query($sqli);  

But it is not working, it does not present an error, it simply does not update the records, I also tried with id , without success.

    
asked by anonymous 16.01.2015 / 03:21

3 answers

2

The problem in the code is in concatenating the SQLs in the $ sqli variable and running all the querys together.

Each query must be executed by itself, so I suggest:

foreach ( $arr as $k => $v ) {
    $sqli = "UPDATE oc_cadastro SET imprime_oc='s', imprime_oc_data='".$pega_data."' WHERE id = '".$ocs_imp[$v]."'";
    $mysqli->query($sqli); 
 } 

I hope to help you.

    
16.01.2015 / 08:27
1

You have some parentesses about in your update:

"UPDATE oc_cadastro SET 
    imprime_oc='s',
    imprime_oc_data='".$pega_data."'
WHERE id = '".$ocs_imp[$v]."')))";
                             ^^^
--------------------------------

I had previously suggested using multi_query () which performs multiple sql-separated statements and comma ( ; ) but this method does not seem to be suitable for INSERT / DELETE / UPDATE because it checks only if the first statement is correct and returns true even if the other statements have errors or it executes all statements until it finds the first wrong and stops and returns true as if everything is right, so records are inserted / updated / removed in half.

To process multiple INSERTS at one time use multiple values in the VALUES ex clause

INSERT INTO tabela (c1, c2, c3) VALUES ('v1', 'v2', 'v3'), ('v12', 'v22', 'v32')

In the DELETE case, just pass the ids in the IN ex:

DELETE FROM tabela WHERE id IN (1,2,3,4,5)

In the case of UPDATE to make sure that all records are modified, turn off auto commit, this means that the source code and not the database is responsible for effecting the rollback transaction for failure and commit for success. Rotate the query using query () inside the foreach.

Options - sprintf

$mysqli->autocommit(false);
$sqli = "";
$erro = false;
foreach ($arr as $k => $v) {
   $sqli = sprintf("UPDATE oc_cadastro SET
                        imprime_oc = '%s',
                        imprime_oc_data = '%s'
                    WHERE id = %d;", 's', $pega_data, $ocs_imp[$v]);

   if(!$mysqli->query($sql)){
      $erro = true;
      echo 'Atualização cancelada: <br>'. $mysqli->errno .' - '. $mysqli->error;
      break;
  }
}

if($erro == true)
   $mysqli->rollback();
else
   $mysqli->commit();

One note:

In the excerpt of the question $k (contains the indexes of the array) is not used in anything, you can then simplify the code a bit and remove it since $item will have the same value as $v . >

foreach($arr as $item){
   " ... WHERE id = %d;", 's', $pega_data, $ocs_imp[$item]);
    
16.01.2015 / 10:56
0

Considering that the return of your $_POST['check_imprime'] is already an array:

$ocs_imp = $_POST['check_imprime'];
$sql = "";

foreach ($ocs_imp as $id => $val) {
   $sql .= "UPDATE .. WHERE id = {$val};";
}

$mysqli->query($sql);
    
16.01.2015 / 04:12