Jump to content


Photo

How do I delete multiple rows with php?


  • Please log in to reply
5 replies to this topic

#1 chrisj989

chrisj989
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 06 July 2006 - 03:48 PM

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.

#2 CamW

CamW
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 July 2006 - 03:53 PM

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.

$ids = getListOfIds();

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

(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.

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

(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

#3 chrisj989

chrisj989
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 07 July 2006 - 05:08 PM

hmm....ok, this is how each row is setup and displayed...

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>";					
				}

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?

#4 CamW

CamW
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 July 2006 - 06:51 PM

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.

#5 chrisj989

chrisj989
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 10 July 2006 - 04:08 PM

so then it would be something like...

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


#6 chrisj989

chrisj989
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 July 2006 - 07:13 PM

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 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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users