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

复选框勾选删除数据库条目功能异常排查及解决

复选框批量删除数据库条目问题及解决方法

问题场景

我开发了一个展示数据库条目的表单,每条条目对应一个复选框,勾选后需要删除该条目。

原表单类代码

// 读取房间数据
$rooms = $DB->get_records_sql( 
    'SELECT LBTB.id, LBTB.datum, LBTB.roomid, LBTB.ustd, 
            LBTB.userid, RAUM.raumname, RAUM.raumnummer 
    FROM {bs_steinweg} LBTB
        LEFT JOIN {bs_raume} RAUM ON LBTB.roomid = RAUM.id
    WHERE roomid > "0"
');

foreach ($rooms as $room) {
    $newdate = date( 'd.m.Y', strtotime( $room->datum ) );
    $mform->addElement('html', '<tr><th scope="row">');
    $mform->addElement('advcheckbox', 'roomid[]','','','', $room->id);
    $mform->addElement('html', '</th><td>');
    $mform->addElement("html", "$room->raumname");
    $mform->addElement('html', '</td><td>');
    $mform->addElement("html", "$newdate");
    $mform->addElement('html', '</td><td>');
    $mform->addElement("html", "$room->ustd");
    $mform->addElement('html', '</td></tr>');
    $mform->addElement('hidden', 'id', $room->id);
    $mform->setType('id', PARAM_INT);
}

原删除逻辑代码

if ( !empty( $fromform->delete ) ) {
    if ( !empty( $fromform->roomid ) ) {
        foreach ( $fromform->roomid as $room_id ) {
            print_r($fromform->id);
            /* $DB->delete_records( 'bs_steinweg', [ 'id' => $room_id ] ); */
        }
        /* $getDeleteCanceld = get_string( 'getDeleteCanceld', 'local_buchungssystem' );
        redirect( $CFG->wwwroot . '/local/buchungssystem/meine_fach_buchungen_sw.php', $getDeleteCanceld ); */
    } else {
        // 处理未选择条目的情况
        $getDeleteCanceld = get_string( 'noRoomsSelected', 'local_buchungssystem' );
        redirect( $CFG->wwwroot . '/local/buchungssystem/meine_fach_buchungen_sw.php', $getDeleteCanceld );
    }
}

遇到的问题

  • print_r($room_id)无任何输出
  • print_r($fromform->id)仅返回最后一条数据的ID,即使勾选第一条条目也是如此
  • 尝试改用for循环处理,仍然没有输出

解决方法

修改表单元素

将复选框的name改为带条目ID的数组形式(原代码中laptopid为笔误,对应场景修正为roomid):

$mform->addElement('advcheckbox', "roomid[$room->id]",'', '', '', $room->id);

修改处理逻辑

更新后的删除验证及执行逻辑:

if ( !empty( $fromform->delroomsw ) ) {
    if ( !empty( $fromform->roomid ) ) {
        $checked_values = $fromform->roomid;
        foreach ( $checked_values as $val ) {
            // 读取对应房间数据
            $rooms = $DB->get_record_sql( "SELECT * FROM {bs_steinweg} WHERE id = $val" );
            if ( $rooms->userid == $USER->id ) {
                $DB->delete_records( 'bs_steinweg', [ 'id' => $val ] );
            } else {
                $getDeleteCanceld = get_string( 'noDeleteRights', 'local_buchungssystem' );
                redirect( $CFG->wwwroot . '/local/buchungssystem/meine_buchungen.php', $getDeleteCanceld );
            }
        }
        $getDeleteCanceld = get_string( 'getDeleteCanceld', 'local_buchungssystem' );
        redirect( $CFG->wwwroot . '/local/buchungssystem/meine_buchungen.php', $getDeleteCanceld );
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 04:34:56