You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP+MySQL按ID删除行异常:所有删除按钮ID固定为同一值

问题解决:AJAX加载的删除按钮全部指向同一ID

问题现象

在searchvehicle.php通过AJAX加载车辆数据时,每行的Delete按钮触发的删除操作都会指向同一个车辆ID,比如某行Edit ID为2时,所有删除按钮都会尝试删除ID为2的车辆。

核心原因

  1. 模态框ID重复:循环生成每行数据时,所有删除模态框都使用了相同的id="myModal",HTML中ID必须唯一,点击任意Delete按钮时,浏览器只会匹配到第一个ID为myModal的模态框,导致所有删除操作复用了同一行的ID。
  2. 表格结构错误:在while循环内部重复输出</thead>标签,破坏了表格的DOM结构,可能引发解析异常。
  3. SQL查询逻辑隐患:WHERE子句中OR和AND混用未加括号,导致查询条件优先级错误,可能返回不符合预期的数据。

修复步骤

1. 给模态框设置唯一ID

为每个车辆的删除模态框生成唯一ID(比如结合车辆ID),同时修改Delete按钮的href属性对应到这个唯一ID。

2. 修正表格结构

将</thead>移出while循环,放在表头行之后,循环内只输出<tbody>中的数据行。

3. 修复SQL查询逻辑

给OR条件添加括号,确保AND条件(用户权限、公司范围)对所有搜索条件生效。

修改后的searchvehicle.php代码

<?php
session_start(); 
require ('../db.php');
$id = $_SESSION['id'];

$return = '';

if(isset($_POST["query"]))
{
    $search = mysqli_real_escape_string($connection, $_POST["query"]);
    // 修复OR条件的括号,确保权限条件生效
    $query = "SELECT vehicle.id, vehicle.vehicle_no, color, model, type, vehicle.comp_id, vehicle.user_id, company.comp_name  
    FROM vehicle
    LEFT JOIN company 
    ON vehicle.comp_id = company.id 
    WHERE 
        (vehicle.id  LIKE '%".$search."%'
        OR vehicle_no LIKE '%".$search."%' 
        OR vehicle.comp_id LIKE '%".$search."%')
        AND FIND_IN_SET( vehicle.comp_id, '".$userprofile['company_id']."' )
        AND vehicle.user_id = '".$id."'
        ";
}
else
{
    $query = "SELECT vehicle.id, vehicle.vehicle_no, color, model, type, vehicle.comp_id, vehicle.user_id, company.comp_name  
    FROM vehicle  
    LEFT JOIN company 
    ON vehicle.comp_id = company.id 
    WHERE FIND_IN_SET( vehicle.comp_id, '".$userprofile['company_id']."' )
    AND vehicle.user_id = '".$id."'
    ";
}

$result = mysqli_query($connection, $query);
$i = 1;

if(mysqli_num_rows($result) > 0)
{
    $return .='
    <div class="table-responsive">
    <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
    <thead class="thead-light">
    <tr>
        <th>ID</th>
        <th>Vehicle Num</th>
        <th>Color</th>
        <th>Model</th>
        <th>Type</th>
        <th>Company Num</th>
        <th>Company Name</th>
        <th>Action</th>
    </tr>
    </thead> <!-- 将thead闭合移出循环 -->
    <tbody>'; <!-- 添加tbody标签 -->
    while($row1 = mysqli_fetch_array($result))
    {
        if($row1['user_id'] == $id)
        {
            $modalId = "myModal-".$row1['id']; // 生成唯一模态框ID
            $return .= '
        <tr>
        <td>'.$i++.'</td>
        <td>'.$row1["vehicle_no"].'</td>
        <td>'.$row1["color"].'</td>
        <td>'.$row1["model"].'</td>
        <td>'.$row1["type"].'</td>
        <td>'.$row1["comp_id"].'</td>
        <td>'.$row1["comp_name"].'</td>
        
        <td>
        <a href="editvehicle.php?id='.$row1['id'].'" class="btn btn-primary btn-sm">Update</a>
        <a href="#'.$modalId.'" class="btn btn-danger btn-sm" data-toggle="modal">Delete</a>
        <div id="'.$modalId.'" class="modal fade">
        <div class="modal-dialog modal-confirm">
            <div class="modal-content">
                <div class="modal-header flex-column">
                    <div class="icon-box">
                        <i class="material-icons">&#xE5CD;</i>
                    </div>                      
                    <h4 class="modal-title w-100">Are you sure?</h4>    
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                </div>
                    <div class="modal-body">
                        <p>Do you really want to delete these records? All activity related to the driver will also be deleted. This process cannot be undone.</p>
                    </div>
                    <div class="modal-footer justify-content-center">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        <a class="btn btn-danger" href="del.php?id='.$row1['id'].'">Delete</a>
                    </div>
            </div>
        </div>
    </div>     
        </td>
        </tr>';
        }
    }
    $return .= '</tbody></table></div>'; // 闭合tbody、table、div
    echo $return;
}
else
{
    echo 'No results containing all your search terms were found.';
}
?>

其他说明

  • del.php代码无需修改,但建议添加错误处理逻辑(比如判断数据库操作是否执行成功)。
  • JS脚本代码无需修改,修复后AJAX加载的内容结构可正常触发对应模态框。

内容的提问来源于stack exchange,提问作者Yamin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 17:30:21