猿问

如何用php更新mysql中的多行?

所以我想用 php 更新 mysql 数据库中的多行,这段代码只更新最后一行。我需要添加什么才能更新所有行?


    <?php


$servername = "localhost";

$username = "root";

$password = "";

$dbname = "cases";


$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


    $Famas_Doomkitty_mn          =       1,54;

    $Famas_Doomkitty_ft          =       1,46;

    $Famas_Doomkitty_mnst          =       2,57;

    $Famas_Doomkitty_ftst          =       2,42;



$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mn' WHERE id=2";


$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ft' WHERE id=3";


$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mnst' WHERE id=7";


$sql = "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ftst' WHERE id=8";




if ($conn->query($sql) === TRUE) {

  echo "Record updated successfully";

} else {

  echo "Error updating record: " . $conn->error;

}


$conn->close();



?>


慕桂英546537
浏览 144回答 1
1回答

互换的青春

您可以尝试使用数组和 foreach,如下所示:$sql_querys = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mn' WHERE id=2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ft' WHERE id=3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_mnst' WHERE id=7",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "UPDATE esports2013skins SET FAMASDoomkitty='$Famas_Doomkitty_ftst' WHERE id=8"&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; foreach($sql_querys as $sql){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($conn->query($sql) === TRUE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Record updated successfully";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Error updating record: " . $conn->error;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答