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.
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.You are not passing the id or the database query syntax is not okay
Use this:
This should work
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.