Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
deleting records with checkboxes
Old 05-04-2005, 03:13 AM deleting records with checkboxes
Novice Talker

Posts: 5
Trades: 0
Hi everyone, I have a problem with a piece of code that I have been working on for the past week. I am new to php; and what I want to do is delete the 'checked' records from the DB.

This is my approach:
PHP Code:
$num_rows mysql_num_rows($result); 
print 
"<br><br>There are $num_rows records.<P>"
print 
"<table width=200 border=0>\n"
while (
$get_info mysql_fetch_row($result)) { 
print 
"<td><input type=\"checkbox\" value=\"" $get_info['idtable'] . "\" name=\"check[]\"></td>\n"
foreach (
$get_info as $field
print 
"\t<td><font face=arial size=1/>$field</font></td>\n"
print 
"</tr>\n"

print 
"</table>\n"

echo 
"<input name=submit type=submit>"


if(isset(
$_POST['submit'])){ 
$ids implode(','$_POST["check"]); 
print 
"<p>List all: $ids</p>"
$sql "DELETE FROM items WHERE idtable IN ($ids)"
if(
$result mysql_query($sql)) echo "Records deleted!"//there are as many records deleted as there are checkboxes checked 

The records don't get deleted... when 'submit' is hit, I get 'List all:' to display, but that is all. No other action takes place. What am I doing wrong?
gartok is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-04-2005, 07:52 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Hi there gartok, this is the exact problem I once had, annoyed the pants off me!! But the solution (annoyingly) is fairly simple!

Basically, we need to get all the information from those checked boxes right? I'm assuming you've used the record ID as the value of each checkbox (but it don't really matter). Below is an example of a HTML tag for a checkbox taken from my own script
HTML Code:
<INPUT TYPE=CHECKBOX NAME="selitems[]" VALUE="14">
You should notice the strange square brackets after the name of the checkbox is set, selitems[] (selitems is just shortened from selecteditems, i know, super clever!).

What will happen now, is when the PHP form is submitted, it will place the value of everything that was ticked into the array, selitems[]. Then we need PHP to handle it!.

PHP Code:
foreach($selitems as $val) {
                
DeleteData("news_articles""news_articles_id""$val");
            } 
We use this loop to go through the $selitems array (note that if global arrays are not on this MUST be $_POST["selitems"]; or $_GET["selitems"]; whatever the send method was) and use it to delete the value in the DB as we want. Each value will be assigned to $val after each loop until it's gone through everything.

Oh yeah, the DeleteData() function won't work for you cause that's a custom function, you just need to replace it with your own SQL Query Code.

I hope this helps you, as I was in a bit of a hurry to go out while posting this!
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-04-2005, 03:18 PM
Novice Talker

Posts: 5
Trades: 0
Thanks for the reply! However, it is a bit confusing for me. I played around with the code a little bit and got it to work by simply changing the . $get_info['idtable'] . to . $get_info[0] .
It seems to work, but I am concerned that it is not the 'proper' way to do it. Would you happen to have any further suggestions?
gartok is offline
Reply With Quote
View Public Profile
 
Old 05-04-2005, 04:57 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
$get_info is a row of your table, and $get_info['idtable'] would try to pick out the value in that row for the idtable field. Does your table have a field by that name? $get_info[0] will get the first field of that row, so if that's the field you want to use, then this a perfectly correct way to do it.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 05-04-2005, 08:39 PM
Novice Talker

Posts: 5
Trades: 0
Yeah, everything works fine. But, I don't understand why it worked with $get_info[0] and it didn't work with $get_info['idtable']? I have the field, and yes it is in the first position.
gartok is offline
Reply With Quote
View Public Profile
 
Old 05-04-2005, 10:08 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
It's because of the
PHP Code:
while ($get_info mysql_fetch_row($result)) 
line, when PHP returns the result of the mysql query, it will place it all in an array.

Depending on what you asked for in the SQL statement (for example, SELECT user_id, user_name, user_pass FROM users WHERE user_id = '23') PHP sets the array accordingly. Using the example in the bracket, if we wanted to get the username from the query, all we have to do is $get_info[1]; because it's the second value we asked for (arrays always start from 0).

It would be completely different if it was mysql_fetch_array however, and for that reason I recommend you read the relevant PHP Manual sections on mysql_fetch_array and mysql_fetch_row
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to deleting records with checkboxes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21838 seconds with 12 queries