www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Deleting Multiple Database Items

  1. #1
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063

    resolved [RESOLVED] Deleting Multiple Database Items

    If you had a blogging engine that you custom wrote, and you wanted to be able to delete multiple entries via a HTML form with check boxes rather than deleting one entry at a time, how would you do this with PHP and SQL?

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    I would build a where statement using PHP and plug that onto my query:

    PHP Code:
    // assuming your ids are in array
    $to_delete = array(1,5,93,7,32);
    $in implode(','$to_delete);
    $sql "DELETE FROM mytable WHERE id IN ($in)"

  3. #3
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    How would the code be able to tell which check boxes were checked? Sorry, but this is the first time I've worked with check boxes on a form.

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    You just name the checkboxes in such a way that they get passed as an array to php, eg:
    PHP Code:
    <form>
    <
    input type="checkbox" name="entries[1]" />
    <
    input type="checkbox" name="entries[2]" />
    <
    input type="checkbox" name="entries[3]" />
    etc...
    </
    form
    Where entries will be your array and the numbers are entry ids. When this is submitted, you will be able to access the array via $_POST['entries]. Any checked checkboxes will be in the array, any unchecked ones wont.

  5. #5
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    How would I do this while fetching the other table fields and displaying them in the form at the same time? I was going to display information about the posts on the delete page, to make it easier to know what you're deleting. However, looking at this, I don't know how to accomplish both at the same time without messing something up.

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    I'm not sure I follow. Could you not just query the database for the post information you wish to show on the delete page, and build your form using checkbox arrays?

    ie:

    PHP Code:
    // get the post data here
    echo '<form method="post" action="foo.php">';
    while (
    $row mysql_fetch_assoc($result)) {
        echo 
    'Delete? <input type="checkbox" name="entries[' $row['id'] . ']" />'
        
    echo '<div>' $row['title'] . '</div>';
        echo 
    '<div>' $row['content'] . '</div>';
    }
    echo 
    '</form>'

  7. #7
    Join Date
    Jan 2009
    Posts
    1
    i m a beginer in php and this thread really very useful for me to learn php using forms.thanks
    PHP Development Company JSP Development company

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles