Jump to content

another checkbox problem


Aezel
 Share

Recommended Posts

In the admin part of my site I want to create a page where admins can delete users (members).I've made a page that calls all members with a checkbox beside every name. If checked the member should be deleted on submit.Here's the code for that page:

<form action="url/members_delete.php" method="post" name="members_del"><?php$sql->QueryRow("SELECT * from members ORDER BY player");for ($i = 0; $i < $sql->rows; $i++) { 	$sql->Fetch($i); 	$player = $sql->data[2]; 	$class = $sql->data[3];	$profone = $sql->data[4];	$proftwo = $sql->data[5];	$delbox = $sql->data[6];?>  <tr>    <td width="20%"> <input type="checkbox" value="1" name="delbox"> --------------></td>    <td width="80%"> <?php echo"$player";?></td>  </tr><?php}?>  <tr>  <td>   <input type="submit" value="Submit"></td>  <td> </td>  </tr></form>

The delbox field is int(1) with a default value of 0.The action page looks like this:

<?php	require("url/php/db.inc.php");	$sql = new MySQL_class;	$sql->Create("mydb");	$sql->Delete("DELETE FROM members WHERE delbox=1");	$affected_rows = $sql->a_rows; header("Location: url/confirm.html");?>

The idea was that when the box is checked it gets the value "1" and all of the checked members would be deleted.I do not get any error messages, it's just that nothing happened when I get to the confirmation page.I think the problem is that the delbox value isn't changed to 1 but I can't see why it isn't.. I have tried an sql update page in between of the form and the sql delete page, to update the delbox value first, but no success there either. Again no error message and no change even tho I get the confirmation page.Can somebody please give me some pointers or even better, show me how to make it work? Thanks :).

Link to comment
Share on other sites

Can you have a value for a checkbox? Anyways, on the action page you should put:

<?$deletebox = $_POST['delbox'];if($delete = !true) { require("url/php/db.inc.php");$sql->Delete("DELETE FROM members WHERE delbox=1");header("Location: url/confirm.html")?>

Plus the rest of the databse connection junk.. I think its right, if not ill edit it.

Link to comment
Share on other sites

Ummm. In the members table in your database, do you have a column with the title delbox? Thats your first mistake, your asking the query to delete all rows where the column (delbox) is equal to 1, which has nothing to do with your checkboxes. My suggestion is to give each checkbox a dynamic name. Your already using a for loop which increments $i each time, well why not combine that with the name delbox and that will be your dynamic checkbox name. Then when you pass the form, also pass the max number of rows in your query, then on the action page have another for loop which goes from 0 to the max rows again. Then, all you do is if ($delbox.$i == "On") { $sql -> DELETE....}else { do nothing}THat would be the best way. Also, checkboxes as you have seen in my If statement, don't return a 0 or 1, but an on or off.

Link to comment
Share on other sites

As an adition to the post befor me, you are already getting every member from the database, what you could do is to get the userid (1,2,3,4) of a user, and name the checkbox the same as the userid!After this you can make a query that looks something like:mysql_query('DELETE player FROM `members` WHERE userid = "'$userid.'"')

Link to comment
Share on other sites

Checkboxes can take any value. In the "value" attribute of a checkbox, you specify which value you want sent to the server if the checkbox is selected. If the checkbox is not selected, no value is sent (i.e. the variable name is not even part of the request). In the example here, if the checkbox is clicked the server receives a variable called "delbox" with a value of "1". If the checkbox is not clicked, the server doesn't receive the "delbox" variable at all.Now for the code. As Selacius said, you need to give each checkbox a unique name. Hopefully you have an 'id' field or something in the database (using $i alone isn't reliable, in case the list of players changes between the time you view the page and the time you submit it).

<form action="url/members_delete.php" method="post" name="members_del"><?php$sql->QueryRow("SELECT * from members ORDER BY player");for ($i = 0; $i < $sql->rows; $i++) {   $sql->Fetch($i);   $id = $sql->data[0];      //I'm not sure of the index, if any, of your ID column  $player = $sql->data[2];   $class = $sql->data[3];  $profone = $sql->data[4];  $proftwo = $sql->data[5];  $delbox = $sql->data[6];?>  <tr>    <td width="20%"> <input type="checkbox" value="1" name="delbox_<?php echo $id; ?>"> --------------></td>

That will attach the id to the name of each checkbox, so they all have different names.The rest of the HTML is the same.For your PHP page, you need to do something similar.

<?phprequire("url/php/db.inc.php");$sql->QueryRow("SELECT id from members");$affected_rows = 0;for ($i = 0; $i < $sql->rows; $i++) {   $sql->Fetch($i);  $id = $sql[0];  $delbox = $_POST["delbox_" . $id];  if ($delbox == "1")  {    $sql2 = new MySQL_class;    $sql2->Create("mydb");    $sql2->Delete("DELETE FROM members WHERE id={$id}");    $affected_rows++;  }}?>

With this, you don't need a page in the middle that updates the value. If you want to delete them, there's no need to update the database, just go ahead and delete them. But this way requires one query per deletion, if you want to group all of them into a single query you could make a list like this:

<?phprequire("url/php/db.inc.php");$sql->QueryRow("SELECT id from members");$ids_to_delete = "";for ($i = 0; $i < $sql->rows; $i++) {   $sql->Fetch($i);  $id = $sql[0];  $delbox = $_POST["delbox_" . $id];  if ($delbox == "1")  {    if ($ids_to_delete != "")      $ids_to_delete .= ",";    $ids_to_delete .= $id;  }}if ($ids_to_delete != ""){  $sql = new MySQL_class;  $sql->Create("mydb");  $sql->Delete("DELETE FROM members WHERE id IN ({$ids_to_delete})");}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...