如何在具有多个ID的mysql中更新多个记录

时间:2019-05-18 08:48:25

标签: php mysql

我有一个html表单,其中包含2 * 12个输入字段,它们的名称分别为name =“ links []”和name =“ ids []” ... 我想使用这12个ID将那12个链接更新为“链接”列 我知道我们需要一个循环。 但是不知道如何进行sql查询。

$ids=mysqli_real_escape_string($conn, $_POST['ids']);
    foreach($ids as $id){....}
$links=mysqli_real_escape_string($conn, $_POST['links']);
    foreach($links as $link){....}

$sql="update query.....";

编辑:

它与两个变量$ id和$ season一起工作,但是当我添加两个以上的变量如$ episode等时,它不起作用。它不执行其他变量,仅执行前两个变量,并将表中所有条目的$ season值设置为1或有时设置为0。

for($i=0 ; $i<count($record['id']); $i++){
  $id=mysqli_real_escape_string($conn, $record['id'][$i]);
  $season=mysqli_real_escape_string($conn, $record['season'][$i]);
  $episode=mysqli_real_escape_string($conn, $record['episode'][$i]);
  $rel_id=mysqli_real_escape_string($conn, $record['rel_id'][$i]);
  $link=mysqli_real_escape_string($conn, $record['link'][$i]);

  //sQl Query
  $sql = "UPDATE series SET season='$season' and episode='$episode' and rel_id='$rel_id' and link='$link' WHERE id='$id'";
    if ($conn->query($sql) === TRUE) {} 
        else { echo "Error: " . $sql . "<br>" . $conn->error; };

3 个答案:

答案 0 :(得分:0)

$ids=mysqli_real_escape_string($conn, $_POST['ids']);
    foreach($ids as $key => $id){
        $sql="update tablename set fieldname=value where link=$_POST[links][$key] and id=$id";
    }

这可能有效。如果不能,请给我更多详细信息。

答案 1 :(得分:0)

尝试以下代码。

<form name="rec" id="rec" method="post">
<input type="text" name="reord[id][]">
<input type="text" name="reord[season][]">
<input type="text" name="reord[episode][]">
<input type="text" name="reord[rel_id][]">
<input type="text" name="reord[link][]">
<br/>

<input type="text" name="reord[id][]">
<input type="text" name="reord[season][]">
<input type="text" name="reord[episode][]">
<input type="text" name="reord[rel_id][]">
<input type="text" name="reord[link][]">

<br/>


<input type="text" name="reord[id][]">
<input type="text" name="reord[season][]">
<input type="text" name="reord[episode][]">
<input type="text" name="reord[rel_id][]">
<input type="text" name="reord[link][]">

</br>

<input type="submit" name="submit"  value="submit">
</form>

<?php
if(isset($_POST['submit'])){
    $record = $_POST['reord'];
    $id = $season = $episode = $rel_id = $link = '';
    for($i=0 ; $i<count($record['id']); $i++){
         $id= mysqli_real_escape_string($conn, $record['id'][$i]);
         $season=mysqli_real_escape_string($conn, $record['season'][$i]);
         $episode=mysqli_real_escape_string($conn, $record['episode'][$i]);
         $rel_id=mysqli_real_escape_string($conn, $record['rel_id'][$i]);
         $link= mysqli_real_escape_string($conn, $record['link'][$i]);
         echo $sql = "UPDATE series SET season='$season' and episode='$episode' and rel_id='$rel_id' and link='$link' WHERE id=$id";
        $conn->query($sql);
    }
}
?>

答案 2 :(得分:0)

UPDATE `tablename` SET `fieldname` = CASE
    WHEN id = 1 AND xyz = 3 THEN `value 1`
    WHEN id = 2 AND xyz = 3 THEN `value 2`
    WHEN id = 3 AND xyz = 3 THEN `value 3`
    END

在上述情况下,我将使用SET-WHEN-THEN查询