Current location: Hot Scripts Forums » Programming Languages » PHP » Deleting All Checked


Deleting All Checked

Reply
  #1 (permalink)  
Old 07-11-05, 05:06 PM
php5 php5 is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Deleting All Checked

I am making a custom portal for one of the users on this forum, dripdown, and part of it is a news system. Part of that news system is the ability for the admin account to delete news articles. I want the administrator to be able to check all boxes that he wants to delete. That part's easy.

But what I need to do is get it to delete all ones that are marked. Here is the current code:
PHP Code:

<?

$root 
'./../';
//Setup page
$pagename 'Add News';
include(
$root 'admin/admin_header.php');
//Begin handling the form.
if($_POST['submit'])
{
    
$db->query($sql);
    
$sql 'DELETE FROM `news` WHERE `title` = \'' $_POST['title'] . '\'';
    
$result $db->query($sql);
    if(
$result)
    {
        
$message 'All selected news entries have been deleted.';
    }
}
?>
<form action="<? $PHP_SELF ?>" method="post">
    <p>
        Select all entries which you wish to delete. <b>This <i>cannot</i> be undone.</b>
    </p>
    <p>
        <?
        $sql 
'SELECT * FROM news';
        
$result $db->query($sql);
        while(
$rows $db->fetchrow($result))
        {
            echo 
'<input type="checkbox" name="' strtolower($rows['title']) . '" value="' $rows['title'] . '" /> ' $rows['title'] . '<BR />';
        }
        
?>
    </p>
    <p>
        <input type="submit" name="submit" class="blink" value=">>Delete News<<" />
    </p>
</form>
<?
//include($root . 'admin/admin_footer.php');
?>
I think I need to somehow do a while look that executes the delete query for every one that is checked. But I don't know how.

If someone could direct me in the right direction, I'd be quite happy.
Thanks in advance,
-php5
Reply With Quote
  #2 (permalink)  
Old 07-11-05, 05:34 PM
sszettella sszettella is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Delete All

Just as you iterated over the rows to create the checkbox names, you will need to iterate over the same result set to "get" the state of the checkbox from the post. In pseudo-code:

PHP Code:

$sql 'SELECT * FROM news';

$result $db->query($sql);
while(
$rows $db->fetchrow($result)) {
  if (
$_POST[$rows['title']] == 'on') {
    
$sql2 'DELETE FROM `news` WHERE `title` = \'' $_POST[$rows['title']] . '\'';
    
$db->query($sq2); 

Note, what this really does, is "get" the title from the DB, then "get" the value of the "title" named checkbox. Then, if the checkbox is checked, the delete query will be executed.

This is one approach that should work for you.
__________________
Steve Szettella
4word systems
http://www.4wordsystems.com
http://szettella.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 07-11-05, 05:45 PM
php5 php5 is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Awesome! Thanks. I'll try it out and reply back here with my results!
Reply With Quote
  #4 (permalink)  
Old 07-11-05, 06:04 PM
php5 php5 is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sszettella
Just as you iterated over the rows to create the checkbox names, you will need to iterate over the same result set to "get" the state of the checkbox from the post. In pseudo-code:

PHP Code:

$sql 'SELECT * FROM news';

$result $db->query($sql);
while(
$rows $db->fetchrow($result)) {
  if (
$_POST[$rows['title']] == 'on') {
    
$sql2 'DELETE FROM `news` WHERE `title` = \'' $_POST[$rows['title']] . '\'';
    
$db->query($sq2); 

Note, what this really does, is "get" the title from the DB, then "get" the value of the "title" named checkbox. Then, if the checkbox is checked, the delete query will be executed.

This is one approach that should work for you.
EDIT: I guess it was just that one bad apple. I removed it in phpMyAdmin and tried again with a new one and it worked. THANKS!


Thanks for your help!
-php5

Last edited by php5; 07-11-05 at 06:08 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[PHP] is it checked? dihan PHP 5 06-16-05 01:36 PM
deleting from db after 5 mins ... simone PHP 1 03-05-04 10:48 AM
Deleting Displayed Text smombour PHP 4 02-18-04 09:45 AM
Deleting data from a database using php hsob PHP 1 02-04-04 01:58 PM
[php] deleting multiple files in directory xeoHosting PHP 1 01-19-04 08:06 AM


All times are GMT -5. The time now is 06:55 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.