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]);
Add in your question the error you’re getting that makes it easier to help you.
– André Ribeiro
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.
– Denis L. Murara
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.
– fpg1503
Blz @Francescoperrotti-Garcia
– Denis L. Murara
Have you checked whether the
$_POST['check_imprime']
is returning the expected data?– Lucio Rubens
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.
– Denis L. Murara
Blz, you already tested the output of
$sqli
directly at the bank?– Lucio Rubens
What is in
$mysqli->error
after you have executed the query?– André Ribeiro