I need to update multiple columns in a single table in a single database. Right now when I run my PHP script it only completes the first UPDATE command, it does not update the entries following it. I'm not sure what is wrong with it:
<?php
$servername = "localhost";
$username = "user";
$password = "pw";
$dbname = "database";
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if($conn->connect_error) {
die("Connection failed:" . $conn->connect_error);
}
$sql="UPDATE ost_help_topic SET team_id='15' WHERE topic_id=10";
$sql="UPDATE ost_help_topic SET team_id='15' WHERE topic_id=17";
$sql="UPDATE ost_help_topic SET team_id='15' WHERE topic_id=22";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
When I run it from command line, it does not error out, it does run the first update just fine. When I go check on the database it has in fact updated the "team_id" to '15' in the "topic_id" column that has the id of "10", but the other two columns are not updated. Why is this?
WHERE topic_id IN (10, 17, 22)to do them all in one query?mysqliis significantly less verbose, making code easier to read and audit, and is not easily confused with the obsoletemysql_queryinterface. Before you get too invested in the procedural style it’s worth switching over. Example:$db = new mysqli(…)and$db->prepare("…")The procedural interface is an artifact from the PHP 4 era whenmysqliAPI was introduced and should not be used in new code.