PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello everyone,
I'm kind of new to PHP in general so please be gentle. I have a function that builds, within an html form, rows each consisting of a checkbox, firstname, and lastname. The idea is that you can check as many boxes as you want, hit a delete button, and then have the selections deleted out of the db. But I'm having trouble actually the info deleted. Here is the code that builds the form (this works fine).
<div class = "warning">
<em>WARNING</em>: Deleting a member cannot be undone!<br>
Select members for deletion.
</div>
<div class = "block_row">
<div class = "checkbox"></div>
<div class = "block"><b>Member ID</b></div>
<div class = "block"><b>First name</b></div>
<div class = "block"><b>Last name</b></div>
</div>
<form method = "POST" action = "">
<?
while (list($memberid, $firstname,$lastname) = mysql_fetch_row($delete_results)) {
echo "<div class = 'block_row'>";
echo "<div class = 'checkbox'><input type = 'checkbox' name = '$memberid'></div>";
echo "<div class = 'block'>".$memberid."</div>";
echo "<div class = 'block'>".$firstname."</div>";
echo "<div class = 'block'>".$lastname."</div>";
echo "</div>";
}
if(isset($_POST['submit'])) {
final_delete();
}
//////////
The final_delete function obviously has to take each $memberid and run a query a simple 'DELETE from db where...' etc. So far my attempts have been unsuccessful. I do know for sure that each checkbox is actually obtaining the correct $memberid b/c I've looked at the source code on the form page. Any input would be GREATLY appreciated. Thanks!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Name the checkbox something like "deleteme[$memberid]" .. It will be available as $_POST['deleteme'] as an array.
Never, ever look for the submit button in the submission data. Instead look for a field that's required to be there, or look at $_SERVER['REQUEST_METHOD'].
Don't assume other fields from the form are there once you know it's a submission. Examine the submission data and verify that it meets your expectations.
It is suggested to use an action of "?" or "#" when you want to submit to the current page instead of blank, which is against standards.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
dudes,
Actually I figured it out right after I made the post yesterday...duh!
I did the check boxes like this:
function final_delete() {
for($i = 0; $i < count($_POST['memberid']); $i++) {
$deletion = $_POST['memberid'][$i];
$name_query = "SELECT firstname, lastname from officers where memberid = '$deletion'";
$name_results = mysql_query($name_query);
$delete_query = "DELETE from officers where memberid = '$deletion'";
$delete_results = mysql_query($delete_query);
if ($delete_results == 1) {
while (list($firstname, $lastname) = mysql_fetch_row($name_results)) {
echo "<span class = 'label'> ".$firstname." ".$lastname." has been removed from the database</span><br>";
}
}
}
This works nicely for batch or single deletes off of my dynamically generated checkboxes.
Thank you both Jaybird and feyd for your input into this matter. I really appreciate your ideas.
Take care...
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
your code is totally uneccesary. In that loop, if you were deleting 10 users, then you will have to run 10 queries, whereas my method only used 1 query if you were deleting 10 or 10,000.