在while循环中删除具有特定ID的行

时间:2018-11-11 09:06:08

标签: php database mysqli

<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button 
class='contact100-form-btn' name='deletem'>Delete Movie</button></a> 
</center></td>";
$_SESSION['delete'] = $row['movieid'];
echo "<td>";
echo"</tr>";
}
echo "</table>";
?>


if (isset($_POST['deletem'])) {
$delete=$_SESSION['delete'];
$query6 = "DELETE FROM movieinfo WHERE movieid=$delete";
mysqli_query($db, $query6);
}

我需要删除具有选定ID的特定行,因此我在会话中存储ID并将其删除,但是当我按Delete按钮时,它仅删除最后一行,因为会话由于循环而存储了所有ID我该如何解决?

1 个答案:

答案 0 :(得分:0)

  <?php
    $sql = "SELECT * from movieinfo";
    $sql_data = mysqli_query($db,$sql);
    echo "<table>";
        echo "<tr>";
            echo "<td><b>Movie Name</b></td>";
            echo"<td><center><b>Delete</b></center></td>";
        echo"</tr>";
        while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
            echo "<tr>";
                echo "<td style='color:white'>";
                echo $row['title'];
                **echo "</td>";
                $mid = isset($_GET['mid']) ? $_GET['mid'] : '';
                $del = "DELETE FROM movieinfo WHERE movieid=$mid";
                mysqli_query($db, $del);
                {
                echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button 
                    class='contact100-form-btn' name='deletem'>Delete Movie</button></a> 
                    </center></td>";
                }
            echo"</tr>";
        }
    echo "</table>";
  ?>