2

I've been trying think of a way to do this. I want it to where users can check off items, hit submit and it goes to the code on the next page and deletes all of the checked items from the database. Problem one is that in the post its only sending over the last checked item. Here is how I have it set up right now.

echo "<form name='fm1' METHOD ='POST' ACTION ='displaydelete.php' > ";

//Draws up the table headers echo ""; echo ""; echo "Fund Number "; echo "Hours "; echo "Percentage"; echo "Delete"; echo ""; //While there are query results data is pushed into table cells while ($row = mysql_fetch_array($queryResult2)) { $hours = $row['hours']; $percentage = $hours / 160 * 100;

   echo "<tr>";
    echo "<td>";
    echo $row['funnumber'];
    echo "</td>";
    echo "<td>";
    echo $hours;
    echo "</td>";
    echo "<td>";
    echo $percentage ."%";
    echo "</td>";
    echo "<td>";
   echo "<input type='checkbox' name='id' value='$row[id]'/>";
  echo "</td>";
    echo "</tr>";




}
    //End of tabel 
echo "</table>";

echo" "; echo "";

What I would like to do is push all of the items into a variable and maybe delete them that way. I'm not really sure how you would handle multiple deletes. I'm doing my delete like this for something else if this helps any.

 $query = "DELETE FROM users

WHERE ninenumber = '$ninenumber'"; $result = mysql_query($query) or die("Query Failed: " .mysql_error());

        mysql_close($conn);
1
  • 1
    Boy, everyone really jumps on these easy ones :D Commented Dec 1, 2010 at 17:16

6 Answers 6

3

In your form:

<input type='checkbox' name='id[]' value='$row[id]'/>

Then, in the file you post to:

if(is_array($_POST['id'])){
   foreach($_POST['id'] as $id){
   ...do something to $id;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of this:

echo "<input type='checkbox' name='id' value='$row[id]'/>";

You need this:

echo "<input type='checkbox' name='id[]' value='$row[id]'/>";

Note the difference. I added [] after the input name. This tells the client and server that there are multiple inputs with that name. $_POST['id'] will be an array you can loop through on the next page.

foreach ($_POST['id'] as $checkbox) {
    // DELETE FROM users WHERE ninenumber = $checkbox
}

isset, is_array, and mysql_real_escape_string omitted for brevity.

1 Comment

agreed that brevity is nice. but when it's this kind of question, they probably like seeing it all spelled out and working together. props on at least mentioning the right ingredients, though :)
1

In the form-generating code, make the name in the html have" []" after it:

...
echo "<input type='checkbox' name='id[]' value='$row[id]'/>";
...

Then, in the form-reading code, your post'ed id will be an array.

$id_array = isset($_POST['id']) && is_array($_POST['id']) ? $_POST['id'] : array();
foreach( $id_array as $id ) {
   $query = "DELETE FROM users WHERE ninenumber = '" . mysql_real_escape_string($id) . "'";
   // execute the delete query
}

Comments

0

Putting [] after the name of a control will turn it into an array in the superglobal that you can then iterate over to get all the values from.

Comments

0

You need to have the same name for all of your checkboxes, then all values are passed as array POST variable.

<input type='checkbox' name='id[]' value='$row[id]'/>

Comments

0

Change

name=id

to

name=id[]

this will then give you an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.