单击按钮从表中删除自定义行

时间:2017-10-06 17:23:19

标签: php database delete-row

此代码对我有用,但点击按钮后,没有任何变化。仅在我手动刷新页面后才能看到删除结果。 有什么可以补充来解决这个问题吗?

<?php
 foreach($results as $value){
     echo "<tr>";
     echo "<td class='posts column-posts'>".$value->id."</td>";
     echo "<td class='email column-email'>".$value->email."</td>";
     echo "<td class='description column-description'><div id='col-container'>".$value->details."</div></td>";
    $delRow = "delete_registration_{$value->id}";
     echo "<td class='posts column-posts'><input type='submit' name= $delRow value='delete'/></td>";
     echo "</tr>";
     if(isset($_POST[$delRow])){
         $wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
     }
}

2 个答案:

答案 0 :(得分:1)

如果要删除行,则需要跳过输出行。像这样:

<?php
 foreach($results as $value){
    $delRow = "delete_registration_{$value->id}";
    if(isset($_POST[$delRow])){
        $wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
    } else {
        echo "<tr>";
        echo "<td class='posts column-posts'>".$value->id."</td>";
        echo "<td class='email column-email'>".$value->email."</td>";
        echo "<td class='description column-description'><div id='col-container'>".$value->details."</div></td>";
        echo "<td class='posts column-posts'><input type='submit' name= $delRow value='delete'/></td>";
        echo "</tr>";
    }
}

答案 1 :(得分:0)

如果要删除该行,则可以避免显示该行。

foreach($results as $value){
    if(isset($_POST[$delRow])){
        $wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
    } 
    else {
        echo "<tr>";
        echo "<td class='posts column-posts'>".$value->id."</td>";
        echo "<td class='email column-email'>".$value->email."</td>";
        echo "<td class='description column-description'><div id='col-container'>".$value->details."</div></td>";
        $delRow = "delete_registration_{$value->id}";
        echo "<td class='posts column-posts'><input type='submit' name= $delRow value='delete'/></td>";
        echo "</tr>";
     }
}