Jump to content

Archived

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

chrisj989

How do I delete multiple rows with php?

Recommended Posts

Hey Guys, first post here and I hope you can help me out with something

I have a checkbox next to each field and what I want to do is allow users to select the rows through the checkboxes and the press an option button to delete the row.  The checkboxes are inserted after every field.

My code is pretty crummy since this is my first time coding. 

Any and all help would be great, thanks!

I'm using PHP with MS SQL 2000 by the way.

Share this post


Link to post
Share on other sites
PHP can interpret data submitted through forms as arrays, so it's pretty easy to treat a series of checkboxes as one array.  You just need to add [] to the end of the element name.  If we assume you have the ids for all your rows when building your checkbox list, it's pretty easy to generate checkboxes that will pass an array of ids to be deleted on a submit.

[code]$ids = getListOfIds();

foreach($ids as $id) {
  echo "<input type=\"checkBox\" name=\"deleteItems[]\" value=\"$id\" /> Row id: $id<br />";
}[/code]

(You can also specify the key name within the brackets, but not specifying is handy here, because PHP will just automatically add the value of a form item to the next index in the array if it isn't specified)

Once the user submits that form, you'll have the list of ids the user checked in either $_GET or $_POST, and you can delete accordingly.  Let's assume it's in $_POST.

[code]foreach($_POST['deleteItems'] as $deleteItem) {
  executeSQL("DELETE FROM yourTable WHERE id = $deleteItem");
}[/code]

(Of course, executeSQL isn't a real command.  You'll need to execute that query using whatever method you use to talk to the database.)

Hope that helps!

-Cameron

Share this post


Link to post
Share on other sites
hmm....ok, this is how each row is setup and displayed...

[code] while ($row=mssql_fetch_row($results))
{

$fields = mssql_num_fields($results);
for( $i = 0; $i < $fields; ++$i){
echo "<td><div align = \"center\">";
echo $row[$i]."</div></td> ";
}
echo "<td><div align = \"center\"><form><input type=\"checkbox\" id= \"rfs[]\"></div></form></td>";
echo "</tr>";
} [/code]

As you can see, the checkboxes are added at the end of each row...but I think they have no relationship to the data, how do I link the checkboxes to the data on the row?

Share this post


Link to post
Share on other sites
You need to set the value attribute of the input tag to be equal to the info about the row you need to delete it.

Share this post


Link to post
Share on other sites
so then it would be something like...

[code]echo "<td><div align = \"center\"><form><input type=\"checkbox\" id= \"rfs[]\" value = "\$row"\ ></div></form></td>";[/code]

Share this post


Link to post
Share on other sites
hmm....well my form is working now, but I'm not able to actually delete the data, here's the full code, maybe someone can point something out that's blantantly  wrong/missing?

[code]
//Code that puts the SQL query into a string...
$query = "SELECT u_fname AS First, u_lname AS Last from tblUsers;";
$query2 = "SELECT u_id AS id FROM tblUsers;";
$results = mssql_query($query);
$id = mssql_query($query2);


//Code that displays the data in a table...

while ($row=mssql_fetch_row($results))
{

$fields = mssql_num_fields($results);
for( $i = 0; $i < $fields; ++$i){
echo "<td><div align = \"center\">";
echo $row[$i]."</div></td> ";
}
echo "<td><div align = \"center\"><input type=\"checkbox\" name = \"$id\"  value = \"$id\"></div></td>";
echo "</tr>";


//Code that TRIES to delete multiple rows of data...

  echo "Before If Statement <br />";  //For Debugging purposes

if(isset($_POST['deleter'])){
foreach($_POST['Delete'] as $del){
$qrem = "DELETE FROM tblUsers WHERE u_fname = $del[0] AND u_lname = $del[1]";
$execute = mssql_query($qrem);
}
echo "Deletion Complete!"; //Debug
}
else
echo "Do Nothing!"; //Debug
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.