Delete a single db row after clicking delete button

I have this query, where I want to delete a single row from DB when clicking on delete button. Right now, it’s deleting the whole DB row. but I want to delete a single row by ID.

Here’s the code

Read More
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>";
}

The code for clicking action is set like this in above code:

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

Am I missing anything here?
Any help will be appreciated.

P.S. I want to delete a row without using jQuery.

Related posts

4 comments

  1. Use this as you are deleting the row if delete_registration is set. You need to set/check it for each row and delete that particular 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. Use this:

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

    This should work

  3. How I do this is in each row I have a hidden input field and have default value = “x”. When user changes any field I use javascript to change the value to “e” and if user clicks “delete” I change the value to “d” and submit the form.

    On Submit I use a switch . Loop through all records, if e – edit, d-delete.

Comments are closed.