I am taking the code from this Page. It is not working well. Both buttons are not working.

From my modified code:
I want to delete the particular id which I was clicked but they delete the last id only. Please check my code.

<?php
/*
Plugin Name: Manage Custom Table
Plugin URI: http://_adddress.com
Description: Testing Plugin
Author: XXXXXXXXXX
Version: 1.0
*/ 

add_action('admin_menu','ManageCustomTable_admin_actions');
function ManageCustomTable_admin_actions() {
    add_options_page('Manage Custom Table','Manage Custom Table','manage_options',__FILE__,'ManageCustomTable_admin');
}
function ManageCustomTable_admin(){
    global $wpdb;
    $data = $wpdb->get_results  ("
                    SELECT
                        id,
                        school_id,
                        added_date,
                        campus_status
                    FROM
                        wp_table_campus 
                ");
    ?>
    <style>.notice-warning {display:none;}</style>

    <table class="widefat" style="margin:20px auto; width:97%;">
        <thead>
            <tr>
                <th>ID</th>
                <th>School ID</th>
                <th>Added Date</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>ID</th>
                <th>School ID</th>
                <th>Added Date</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
        foreach ($data as $data) {
            echo '<tr>';
            echo '<td>'. $data->id.'</td>';
            echo '<td>'. $data->school_id.'</td>';
            echo '<td>'. $data->added_date.'</td>';
            echo '<td>'. $data->campus_status.'</td>';
            echo '<td>'; 
            ?>           
    <form method="post" enctype="multipart/form-data">           
   <input type="submit"  name="delete" value="delete"  />  
    </form>
            <?php
            echo '</td></tr>';
        }
        ?>          
        </tbody>
    </table>
<?php   
 $ids =  $data->id;       
if (isset($_POST['delete'])) {
         $wpdb->query(           
              'DELETE FROM wp_table_campus WHERE id= "'.$ids.'"'
        );
    }
?>
<?php
}
?>

Read more here: Edit, Update, Delete – fetch wp database table to display and update on wordpress dashboard


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: