douzhi3105 2019-07-08 09:32
浏览 38

为什么查询只删除最后一行?

<?php
$query= mysqli_query($link, "SELECT * FROM Messages");
while ($row=mysqli_fetch_assoc($query)){
  $MID= $row['mesgID'];
?>
<br><br><br><center><caption><?php echo "The <strong>".$row['type']."</strong> ID: ".$row['mesgID']; ?></caption></center>
<center><table border='4'; style="border-color:darkblue;">
<tr>
    <td style="width:70%; text-align:left;"><?php echo "<strong>From: </strong>".$row['email']."<br><strong>About: </strong>".$row['areaCode']; ?></td>
</tr>
    <tr>
        <td style="width:70%; text-align:center;"><?php echo "<strong>Message content</strong><br><br>".$row['message']."<br>"; ?></td>
    </tr>
</table></center>

 <form action="ContactCleanersManagementStaffSide.php" method="post">
<center>
  <input class="button button3" type="submit" name="<?php echo $MID; ?>"value="DELETE" id="submit"/></center>
    </form>
<br><br>
<?php
}


if(isset($_POST[$MID])){

    $query = mysqli_query($link, "DELETE FROM Messages WHERE mesgID='$MID'");
}

    ?>

I create "delete" button with unique name for each button, but when i clicks any button -except the last button- it's not deleting, but when i click the lats button, it's deleted seccessfully.

  • 写回答

2条回答 默认 最新

  • duane9322 2019-07-08 09:42
    关注

    Instead of using id as name attriute of button, i will ask you to create a hidden filed with name="mesgID" and value="<?php echo $row['mesgID']; ?>" like below:

    <?php
    
        $query= mysqli_query($link, "SELECT * FROM Messages");
        while ($row=mysqli_fetch_assoc($query)){
    ?>
        <br>
        <br>
        <br>
        <center>
            <caption><?php echo "The <strong>".$row['type']."</strong> ID: ".$row['mesgID']; ?></caption>
        </center>
        <center>
            <table border='4'; style="border-color:darkblue;">
                <tr>
                    <td style="width:70%; text-align:left;">
                        <?php echo "<strong>From: </strong>".$row['email']."<br><strong>About: </strong>".$row['areaCode']; ?>
                    </td>
                </tr>
                <tr>
                    <td style="width:70%; text-align:center;">
                        <?php echo "<strong>Message content</strong><br><br>".$row['message']."<br>"; ?>
                    </td>
                </tr>
            </table>
        </center>
    
        <form method="post">
            <input type="hidden" name="mesgID" value="<?php echo $row['mesgID']; ?>">
            <center>
                <input class="button button3" type="submit" name="Delete" value="DELETE" id="submit"/>
            </center>
        </form>
        <br>
        <br>
    <?php }
    
        if(isset($_POST['mesgID'])){
            $query=mysqli_query($link, "DELETE FROM Messages WHERE mesgID=".$_POST['mesgID']);
        }
    ?>
    

    Note:- Current code is wide-open for SQL INJECTION so use prepared statements

    Additional Note: A 'quick' fix [only] for SQL Injection is to force type casting to integer for numeric id values.

    $query=mysqli_query($link, "DELETE FROM Messages WHERE mesgID=".(int)$_POST['mesgID']);
    

    The above is NOT a replacement for leveling up to parameterised Queries.

    References:-

    mysqli::prepare

    PDO::prepare

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度