Jump to content

Deleting Multiple Rows With Check Boxes


Greysoul

Recommended Posts

Having some trouble getting my multiple delete check boxes to actually delete. Everything displays properly. When i select them and press delete it refreshes the page as if it were a success, but the items really haven't been deleted. Any idea what i'm doing wrong here?

<?php$host="localhost";$username="user"; $password="pass";$db_name="db"; $tbl_name="Players"; mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");$sql="SELECT * FROM $tbl_name";$result=mysql_query($sql);$count=mysql_num_rows($result);?><table width="400" border="0" cellspacing="1" cellpadding="0"><tr><td><form name="form1" method="post" action=""><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr><td bgcolor="#FFFFFF"> </td><td colspan="4" bgcolor="#FFFFFF"><strong>Players</strong> </td></tr><tr><td align="center" bgcolor="#FFFFFF">#</td><td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td><td align="center" bgcolor="#FFFFFF"><strong>Player</strong></td><td align="center" bgcolor="#FFFFFF"><strong>Clan Name</strong></td></tr><?phpwhile($rows=mysql_fetch_array($result)){?><tr><td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['ID']; ?>"></td><td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td><td bgcolor="#FFFFFF"><? echo $rows['Player']; ?></td><td bgcolor="#FFFFFF"><? echo $rows['ClanName']; ?></td></tr><?php}?><tr><td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td></tr><?if($delete){for($i=0;$i<$count;$i++){$del_id = $checkbox[$i];$sql = "DELETE FROM $tbl_name WHERE ID='$del_id'";$result = mysql_query($sql);}// if successful redirect to playerinputtest.phpif($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=playerinputtest.php\">";}}mysql_close();?>

Link to comment
Share on other sites

First, you're trying to delete rows after showing the list, not before. You should delete whoever is marked before printing the data, so that when you print the data it doesn't include the rows you just deleted.Also, you're relying on register globals being enabled, you aren't getting any form data yourself. Since your form is submitting via post, you need to get all of the form data, including the checkboxes, from $_POST. Don't expect the variables to get created for you, you need to get the values manually.

Link to comment
Share on other sites

First, you're trying to delete rows after showing the list, not before. You should delete whoever is marked before printing the data, so that when you print the data it doesn't include the rows you just deleted.
yes i'm trying to delete the rows after i show the list, else how can i know who is marked if i cannot print the data to mark them? i'm probably really confused right now lol.
Also, you're relying on register globals being enabled, you aren't getting any form data yourself. Since your form is submitting via post, you need to get all of the form data, including the checkboxes, from $_POST. Don't expect the variables to get created for you, you need to get the values manually.
gotcha
Link to comment
Share on other sites

yes i'm trying to delete the rows after i show the list, else how can i know who is marked if i cannot print the data to mark them? i'm probably really confused right now lol.
Here are the order that things happen in:1. You go the page.2. The code on the top grabs the database info.3. You start printing the form, then loop through the rows and print them.4. After the form prints, it checks for $delete, but it's not defined at this point so the script ends.5. You fill out the form and hit submit.6. The page loads again.7. The code on the top grabs the database info.8. You start printing the form, then loop through the rows and print them.9. You check for deleted rows, and delete themSo notice that you have 2 requests. You load the page once to show the form, then you submit which loads the page again. The second time the page loads, and the form is submitted, you should be deleting the users at that point before printing the updated list. So, your delete code should be on top, even before you grab the data to display. On the top of the script you should check to see if the form was submitted, and process the data if it was. If it wasn't submitted, then you just display as normal.The easiest way of checking for submission is to use isset:if (isset($_POST['delete'])){// form was submitted}Anything in $_POST will only be set if the form was submitted.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...