1
If you will update the field with the same value for all id’s, just use the example of Jéferson Bueno (in the comments):
update tabela set campo = 'valor' where id in ('id1', 'id2', 'id3')
If each id receives a different value, you will need to use multi-query.
Example:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* verifica conexão */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "update tabela set campo = 'Novo_a2' where id='id1';";
$query .= "update tabela set campo = 'Novo_c2' where id='id3';";
$query .= "update tabela set campo = 'Novo_d2' where id='id4';";
/* executa a sua multi query */
if ($mysqli->multi_query($query)) {
do {
/* Faz print separado do resultado de cada query */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* separa cada resultado por traços */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* fecha conexão */
$mysqli->close();
?>
Do you want to know how the update looks? If so, try doing it that way:
update tabela set campo = 'valor' where id in ('id1', 'id2', 'id3')
.– Jéf Bueno
@Jéfersonbueno in this case will update all id’s with the same value, in the example he gave each ID gets a different value. I think he will need to use multi-query http://php.net/manual/mysqli.multi-query.php
– Filipe Moraes
@True Filipemoraes, really had not noticed this detail in the example.
– Jéf Bueno
Depending on the table structure, I usually prefer to use REPLACE INTO, which supports multiple values in a single query, just like it works in INSERT. It’s more performative than doing several updates or even using the multi_query of mysqli. I won’t comment on sending the data to PHP. I think at least , the basic HTML you should know..
– Daniel Omine