1

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

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.

2
  • When you said the whole DB row, did you mean the whole table is deleted? Commented Oct 7, 2015 at 9:09
  • Yes, the whole table is deleted right now. Commented Oct 7, 2015 at 9:10

4 Answers 4

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

}
Sign up to request clarification or add additional context in comments.

9 Comments

wait I'll give it a try.
nothing is happening now. The page just gets reloaded but a row not deleted.
are you having a delete button for each row ? Also check the updated answer.
Yes.. you can see the screenshot here awesomescreenshot.com/image/639957/…
|
0

You are not passing the id or the database query syntax is not okay

1 Comment

Query syntax is fine, just cannot delete the single row.
0

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

Comments

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.