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