Deleting single item with submit from database results in two ID’s and strings

When I try and delete an entry from the code below it seems to spew out two array values with different index numbers and will not delete the entry. If I click multiple times it finally gets the correct ID but only by some random chance.

Using echo var_dump($deleted) results in something like:

Read More

String(2) “10”
String(1) “7”

These can vary and seems to be random.

I have reduced my whole plugin to just the code below thinking that maybe some other submit query might be interfering with it. And still I get two ID’s with unknown string assignments.

Any ideas?

    function myplugin_update_page(){

        global $wpdb;

        $update_page_list = $wpdb->get_results('SELECT * FROM wp_update_page');
        $active = $wpdb->get_var("SELECT * FROM wp_update_page WHERE active='on'");

        echo '<form method="post" action=""><table>';

        foreach ( $update_page_list as $update ) { ?>

            <tr>
                <td><input type="image" src="<?php echo plugin_dir_url(__FILE__).'images/delete.png'; ?>" name="delete[]" value="<?php echo $update->ID; ?>" /></td>
            </tr>
            </table>
        </form>
        </div>

            <?php 
        } 


        if(isset($_POST['delete'])) {
            $delete = $_POST['delete'];

            foreach ($delete as $deleted) {
                $wpdb->query("DELETE FROM wp_update_page WHERE ID = $deleted");
            }
            echo "<script type='text/javascript'>window.location=document.location.href;</script>";
        }
    }

Related posts

Leave a Reply