How to update multiple records at once with just one click with php / mysql?
Example:
If you will update the field with the same value for all the 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 is given 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();
?>