Multiple UPDATE from a dynamic field

Asked

Viewed 736 times

3

I’m trying to catch the ids that come by user-selected checkbox (array), and through these ids, update 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);  

However not working, no error, simply does not update the records, also tried with WHERE id IN ($id), unsuccessful.

  • Add in your question the error you’re getting that makes it easier to help you.

  • Thank you for answering André, there is no error printed on the screen, the procedure is the system take the ids and open another page, and at the same time update as in the code posted, but it opens the other screen with the ids, but does not update as code.

  • 1

    I have removed the greetings and thanks from your question. Understand the reason here. I removed the tags from the title of your question. Understand why here.

  • 1

    Blz @Francescoperrotti-Garcia

  • Have you checked whether the $_POST['check_imprime'] is returning the expected data?

  • Already @luciorubeens, so much so that with the id s recovered, it performs another function that does not come to the case, and it is at this time that it should update and does not, I am in doubt if this code is correct, or if there is another way.

  • Blz, you already tested the output of $sqli directly at the bank?

  • 1

    What is in $mysqli->error after you have executed the query?

Show 3 more comments

3 answers

2


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

Each query should be executed alone this way 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 you helped him.

1

Have some relatives about in your update:

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

Had previously suggested the use of multi_query() which executes several sql statements separated by semicolon(;) but this method does not seem suitable for INSERT/DELETE/UPDATE because it checks only if the first instruction is correct and returns true even if the others have errors or is it performs all instructions until find the first wrong and to and returns true as if everything is right, with that records are inserted/updated/removed by half.

To process multiple INSERTS at once use multiple values in clause VALUES ex:

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

In the case of DELETE just pass the ids in the client 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 the 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. Run 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();

A remark:

In the excerpt from the question $k(contains the contents of the array) is not used at all, so you can simplify the code a little and remove it because $item shall have the same value as $v.

foreach($arr as $item){
   " ... WHERE id = %d;", 's', $pega_data, $ocs_imp[$item]);
  • Perfect @rray, how it always worked perfect. Thanks again, did not know these commands rollback and commit, living and learning! Thanks!

  • @Denisl.Murara This is called a two-phase transaction, some banks leave by default the option auto commit as true others force you to give rollback/commit. Still talking about transactions you can search for some related issues like, isolation levels, ACID and check save/point that a rollback/commit add-on.

0

Whereas 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.