|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
delete posts checkboxes & PHP
Okay I am trying to make some posts deletable with checkboxes. I have already set up the page so it does a while loop and displays the post, and I even set it up with individual links for deletion where it posts the id through the url, but I want to let users be able to check all and then delete them. Can this be done with PHP easily? It seems difficult since the page is doing a loop, so it is basically getting information, posting it, and then replacing that same info with new info to post, thus when I use the $_POST check it displays the last info retrieved. Any ideas... i tried arrays but I'm new to those and they don't seem to work...
thanks
__________________
hey it's the CHARKING |
|
#2
|
||||
|
||||
|
Any time I've done this, I've named the checkbox something like delete_00001, where 00001 is the id number of the given post. If no other fields are named delete_<number>, you can loop through the $_POST array and, for any field name that has the format delete_<number>, do a preg_replace (converting "delete_" to "") and issue a delete statement for the id in question. You'll want to be careful about validating user input here, though, lest somebody just send you a big POST or GET that maliciously deletes all your data. Maybe check your referer or run intval() on each id before executing the query to make sure there's no extra stuff being passed. You might also consider limiting the number of deletions that can be performed at once to the number of items displayed per page as an extra precaution. Something else you might consider is not deleting, but adding a flag to your database that you set to false if somebody performs a deletion.
|
|
#3
|
|||
|
|||
|
how can I do this? I can't figure out how to loop through results that are made dynamically like that... this is my code...
PHP Code:
thank you for you quick reply, I will try to figure this one out on this end still, but I jsut don't know what to do next. Last edited by thecharking : July 21st, 2003 at 12:07 PM. |
|
#4
|
||||
|
||||
|
To loop through your POST vars by name, try something like the following:
PHP Code:
The code looks at each POST variable and checks to see if the field name begins with "delete_." If it does, it pushes a query onto the $queries array for use later. To build the query, we run the preg_replace() function, getting rid of the "delete_" and making sure we're working with an integer. In this example, it doesn't matter what value you give the checkbox. The checkbox will only be passed to the $_POST array if it's checked. |
|
#5
|
||||
|
||||
|
Note that you could probably also just name your checkbox for each entry the same thing, with brackets at the end of the name so it's treated like an array, and then do a foreach on that field. So:
PHP Code:
|
|
#6
|
|||
|
|||
|
Take a look at this sample which will display an array with enough information to create the proper delete sql statement after you check some boxes and press the delete button.
As a side note, dhouston made a comment about just setting a flag as opposed to actually deleting... I usually never delete anything unless there are compelling reasons to do so.
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#7
|
|||
|
|||
|
i know it's been a while but...
Okay I'm sorry I didn't post this earlier, (after you gave the help), it has been a while. I am still having trouble with this! I can't seem to get it to work. I am really not very familiar with array's... and I don't want to be a bother, but can you explain how I can get each part of the delete array to be deleted? I hope this question isn't too old. I just decided to move on to less frustrating things when I couldn't get it initially; I thought I would jsut come back later and figure it out; but I stillc can'tfigure it out. Hope you can help. thank you very much.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > delete posts checkboxes & PHP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|