• 0

[php] Checkbox delete records


Question

I'm struggling to find a good example of using checkboxes to delete records from a database. I currently have a table that I would like to display to the user which they can then select multiple entries from and delete. So far I have this small piece of code which takes the data from the table and assigns a checkbox to it but I don't really have an idea how to take it further.

<? while($info = mysql_fetch_array($data))
	{
		echo "<tr>";
		echo "<td>".$info['genre'] . "</td><td><input type = checkbox name = dgenre value =".$info['genre_id'] . "></td>";
		echo "</tr>";
	}
?>

Please help!

Edited by ciba
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
I'm struggling to find a good example of using checkboxes to delete records from a database. I currently have a table that I would like to display to the user which they can then select multiple entries from and delete. So far I have this small piece of code which takes the data from the table and assigns a checkbox to it but I don't really have an idea how to take it further.

<? while($info = mysql_fetch_array($data))
	{
		echo "<tr>";
		echo "<td>".$info['genre'] . "</td><td><input type = checkbox name = dgenre value =".$info['genre_id'] . "></td>";
		echo "</tr>";
	}
?>

Please help!

Hmm well im not 100% sure.. but when you assign the name to the checkbox.. why not give the checkbox the name of the genre or um how about name= "d$info['genre']", this way each checkbox name is unique and specific to that row of info.Then when the form is submitted, try some sorta loop and check to see if one of them is set. Im not sure if this is the best way, but thought i would try and help get ideas in your head...

Link to comment
Share on other sites

  • 0

Ok,

HTML:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title></title>
	</head>
	<body>
		<form method="post" action="process.php">
			<input type="checkbox" name="recordsToDelete[]" value="1" />
			<input type="checkbox" name="recordsToDelete[]" value="2" />
			<input type="checkbox" name="recordsToDelete[]" value="3" />
			<input type="checkbox" name="recordsToDelete[]" value="4" />
			<input type="checkbox" name="recordsToDelete[]" value="5" />
			<input type="checkbox" name="recordsToDelete[]" value="6" />
			<input type="submit" value="Submit" />
		</form>
	</body>
</html>

PHP:-

<?php
if(count($_POST['recordsToDelete']) > 0)
{
	foreach( $_POST['recordsToDelete'] as $key => $recordID )
	{
		// Do Something with $recordID.
	}
}
?>

That should get you started.

Good luck,

SilverB. :cool:

Link to comment
Share on other sites

  • 0

Cheers guys. I managed to do it quite simple in the end using:

if(isset($_POST['delete']))
{
	foreach($_POST['delete'] as $value) 
	}
	$sql_query = mysql_query("DELETE FROM genre WHERE genre_id = '$value'");
	}
}

Which does the job nicely after changing the checkbox names to delete[] to form an array of values.

Link to comment
Share on other sites

  • 0
Cheers guys. I managed to do it quite simple in the end using:

if(isset($_POST['delete']))
{
	foreach($_POST['delete'] as $value) 
	}
	$sql_query = mysql_query("DELETE FROM genre WHERE genre_id = '$value'");
	}
}

Which does the job nicely after changing the checkbox names to delete[] to form an array of values.

Workable but this would I think this is better:

if(isset($_POST['delete']))
{
				$deletelist="";
				$deletecounter=0;
	foreach($_POST['delete'] as $value) 
	{
					$deletelist+="'$value$'";
					if $deletecounter>0 $deletelist+=",";
					deletecounter++;
	}
	$sql_query = mysql_query("DELETE FROM genre WHERE genre_id in ($deletelist)");
}

This means that even if you try to delete 10,000 records, you end up with one hit on the database rather than 10,000.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.