Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,582 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,913 people online right now. Registration is fast and FREE... Join Now!




deleting checkboxes

 
Reply to this topicStart new topic

deleting checkboxes

ghqwerty
28 Sep, 2008 - 01:17 PM
Post #1

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 341



Thanked: 8 times
Dream Kudos: 25
My Contributions
im making an inventory page and i have selected items from the database in a while loop and on each line added a checkbox at the end like below
im pretty sure id have to somehow give each checkbox a differant value so that i could do a check to see if it is checked by putting it into some kind of array but not sure how and ive looked at 4/5 sites and cant find what im looking for and im tired so ive come back to you guys smile.gif

p.s. sorry about not indenting but if i didnt it wouldnt fit into the screen
php

<?php
$sql = "select * from items where userid = '".$_session['id']."' ";
$result = mysql_query("select * from items where userid = '".$_SESSION['id']."' ") or die('summit went wrong');
$count = mysql_num_rows($result);
if($count <1){
print("<tr><td colspan=\"2\"><center>You currently have no items.<center></td></tr>");
}else{
while($item = mysql_fetch_array($result)){
print("<tr><td><center>".$item[item]."</center></td><td width=\"40%\"><center>".$item[itemid]."</center></td><td width=\"10$\">
<form method\"_Get\" action=\"inventory.php\">
<input type=\"checkbox\" name=\"delete\" value=".$item[itemid].">
</td></tr>
<tr><td colspan='3'><p align='right'><input type='submit' name='delete' value='delete!'</p></td></tr>");
}
}
if(isset($_GET['delete'])){
$delete = mysql_query("delete from items where itemid = ".$item[itemid]." ") or die('crap');
}
?>

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Deleting Checkboxes
28 Sep, 2008 - 02:12 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,995



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
You will probably want to start the form outside the loop, otherwise the open form tag will be printed for each item in the array. Also you do not have a </form> tag anywhere.

what is the output you get? What exactly isn't working?


**scrollbars are magic, please leave the indenting in next time.
User is offlineProfile CardPM
+Quote Post

ghqwerty
RE: Deleting Checkboxes
29 Sep, 2008 - 08:19 AM
Post #3

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 341



Thanked: 8 times
Dream Kudos: 25
My Contributions
arrk sorry smile.gif lol

no i need to have the form start inside the loop so that every item gets a checkbox so that i can check whatever boxes i like press the delete button which then deletes the selected rows

the </form> and <input type="submit" name="delete" value="delete!"> is outside the loop as this only need to happen once

what i need is a way to be able to delete what ever boxes are checjed
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Deleting Checkboxes
29 Sep, 2008 - 08:29 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 396



Thanked: 37 times
Dream Kudos: 75
My Contributions
Wee present from me to you smile.gif

*EDIT I made a mistake by typing AS instead of as - full code can be found 2 posts down smile.gif

CODE

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php  
//You need the delete above the select to show your changes instantly
if(sizeof($_GET['delete'])){  
    foreach($_GET['delete'] AS $dId){
        $delete = mysql_query("delete from items where itemid = ".(int)$dId) or die('crap');  //Adding an (int) will help avoid SQL errors
    }
}
//$sql = "select * from items where userid = '".$_session['id']."' ";  - doesn't seem to be used
$result = mysql_query("select itemid, item from items where userid = ".(int)$_SESSION['id']) or die('summit went wrong');  // changes the userid = to an integer input, and added only the required columns to the SQL select to help speed up your system
$count = mysql_num_rows($result);  
if($count <1){  
    print("<tr><td colspan=\"2\"><center>You currently have no items.<center></td></tr>");  
}else{  
    while($item = mysql_fetch_array($result)){  
        print("<tr><td><center>".$item['item']."</center></td><td width=\"40%\"><center>".$item['itemid']."</center></td><td width=\"10$\">  
        <input type=\"checkbox\" name=\"delete[]\" value=".$item['itemid'].">  
        </td></tr>  
        <tr><td colspan='3'><p align='right'><input type='submit' name='delete' value='delete!'</p></td></tr>");  
    }  
}
?>  
<input type="submit" name="del_btn" id="del_btn" value="Delete" />
</form>


Basically, you assign your checkbox names to an array which is posted, then using a foreach you can loop through each checkbox and pull its value, removing it from the database. I updated the SQL statement so that you are only requesting column names that are required at this stage as it will speed up your application (you should never really use the SELECT *).

In the WHERE clause, I removed the single quotes from the userid = as I assumed this was an int field - if this is wrong just undo my changes on that section.

Hope it works out well for ya smile.gif

This post has been edited by pemcconnell: 30 Sep, 2008 - 05:24 AM
User is offlineProfile CardPM
+Quote Post

ghqwerty
RE: Deleting Checkboxes
29 Sep, 2008 - 09:11 AM
Post #5

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 341



Thanked: 8 times
Dream Kudos: 25
My Contributions
right so ive changed it a little
CODE

                            while($item = mysql_fetch_array($result)){  
                                print("<tr><td><center>".$item['item']."</center></td><td width=\"40%\"><center>".$item['itemid']."</center></td><td width=\"10$\">  
                                <input type=\"checkbox\" name=\"delete[]\" value=".$item['itemid'].">  
                                </td></tr>  
                                ");  
                            }
                                print("<tr><td colspan='3'><p align='right'><input type='submit' name='delete' id='delete' value='delete!'</p></td></tr>");
                        }


but even without the changes i get
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\inventory.php on line 56
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Deleting Checkboxes
30 Sep, 2008 - 12:57 AM
Post #6

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 396



Thanked: 37 times
Dream Kudos: 75
My Contributions
oops! my bad - it's as instead of AS in the foreach(), i.e.:

CODE

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php  
//You need the delete above the select to show your changes instantly
if(sizeof($_POST['delete'])){  
    foreach($_POST['delete'] as $dId){
        $delete = mysql_query("delete from items where itemid = ".(int)$dId) or die('crap');  //Adding an (int) will help avoid SQL errors
    }
}
//$sql = "select * from items where userid = '".$_session['id']."' ";  - doesn't seem to be used
$result = mysql_query("select itemid, item from items where userid = ".(int)$_SESSION['id']) or die('summit went wrong');  // changes the userid = to an integer input, and added only the required columns to the SQL select to help speed up your system
$count = mysql_num_rows($result);  
if($count <1){  
    print("<tr><td colspan=\"2\"><center>You currently have no items.<center></td></tr>");  
}else{  
    while($item = mysql_fetch_array($result)){  
        $tableinfo .= "\t<tr><td><center>".$item['item']."</center></td><td width=\"40%\"><center>".$item['itemid']."</center></td><td width=\"10$\">";
        $tableinfo .= "<input type=\"checkbox\" name=\"delete[]\" value=".$item['itemid']."/></td></tr>\n";
    }
    echo $tableinfo;
    echo '<tr><td colspan="3"><p align="right"><input type="submit" name="delete" id="delete" value='delete!'</p></td></tr>';
}
?>  
<input type="submit" name="del_btn" id="del_btn" value="Delete" />
</form>


Sorry bout that dude - type too fast for my own good sometimes smile.gif

This post has been edited by pemcconnell: 30 Sep, 2008 - 01:03 AM
User is offlineProfile CardPM
+Quote Post

ghqwerty
RE: Deleting Checkboxes
30 Sep, 2008 - 07:36 AM
Post #7

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 341



Thanked: 8 times
Dream Kudos: 25
My Contributions
as i suspected and still doesnt work

AS and as will work the same its something else .. seeing as though i dont understand a foreach loop i dont know im off to tizag
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:34AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month