单击“删除”按钮后删除单个数据库行

时间:2015-10-07 08:58:28

标签: php mysql database wordpress

我有这个查询,我想在点击删除按钮时从数据库中删除一行。现在,它正在删除整个数据库行。但我想通过 ID 删除一行。

以下是 code

add_action('admin_menu', 'conference_register');
function conference_register(){
      add_options_page('Register Conference', 'Conference Registration',  'manage_options', 'conference-registration', 'display_conference');
}

function display_conference(){
    echo "<form method='post'>";
    echo "<div class='wrap'>";
    echo "<h2>Conference Registration User Details</h2>";
    global $wpdb;
    $results = $wpdb->get_results("SELECT `id`,`email`,`details` FROM `conference_register`"); ?>
    <table class="wp-list-table widefat fixed striped pages">
      <thead>
          <tr>
            <th id="posts" class="manage-column column-posts num">ID</th>
            <th id="email" class="manage-column column-email">Email</th>      
            <th id="description" class="manage-column column-description">Details</th>
            <th id="posts" class="manage-column column-posts num">Delete</th>
          </tr>
      </thead>
    <tbody id="the-list">
   <?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>";
        echo "<td class='posts column-posts'><input type='submit' name='delete_registration' value='delete'/></td>";
        echo "</tr>";
         if(isset($_POST['delete_registration'])){
            $wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
        }

    }
    echo "</tbody></table></div></form>";
}

点击操作的code在上面的代码中设置如下:

 if(isset($_POST['delete_registration'])){
    $wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
}

我在这里遗漏了什么? 任何帮助将不胜感激。

P.S。我想在不使用 jQuery 的情况下删除行。

4 个答案:

答案 0 :(得分:1)

如果设置了delete_registration,则在删除行时使用此选项。您需要为每一行设置/检查它并删除该特定行。

<?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 ) );
    }

}

答案 1 :(得分:0)

您没有传递id或数据库查询语法不正常

答案 2 :(得分:0)

使用此:

 echo "<td class='posts column-posts'><input type='submit' name= 'delete_registration_{$value->id}' value='delete'/></td>";
 echo "</tr>";
  if(isset($_POST[delete_registration_{$value->id}])){
    $wpdb->delete( 'conference_register', array( 'id' => $value->id ) );
 }

这应该有效

答案 3 :(得分:0)

我是如何做到的,每行都有一个hidden input字段,默认值为&#34; x&#34;。当用户更改任何字段时,我使用javascript将值更改为&#34; e&#34;如果用户点击&#34;删除&#34;我将值更改为&#34; d&#34;并提交表格。

在提交时我使用switch。循环浏览所有记录,如果是电子编辑,则删除。