954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with delete multiple rows in mysql using checkboxes

please help i don't know how to delete multiple rows in mysql using checkboxes, because it seems the delete doesn't seem to work.
here's the code:

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
fortiz147
Newbie Poster
18 posts since Aug 2008
Reputation Points: 11
Solved Threads: 0
 

see this code will work at this url:
http://www.daniweb.com/forums/thread134908.html

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

it won't help sorry.. coz im lookin for the delete function not validation

fortiz147
Newbie Poster
18 posts since Aug 2008
Reputation Points: 11
Solved Threads: 0
 

hello..
see that code is not for validating...
for checking checkboxes only...
its working fine for me...

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

yep but i need the delete function for mysql >.< i dont know how to query the data from mysql in php when using the checkboxes in each row.

fortiz147
Newbie Poster
18 posts since Aug 2008
Reputation Points: 11
Solved Threads: 0
 

ya,this is the continuation code for the above post...
after getting all check boxes you checked,then write this query to delete them(checked)

if(isset($_POST['check_compare']))
{
$tempids=$_POST['check_compare'];	
echo $tempids;

$ser_qry="delete  from sometable where find_in_set(p_id,'".$_POST['check_compare']."')";
$ser_res=mysql_query($ser_qry);


}
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

yep but i need the delete function for mysql >.< i dont know how to query the data from mysql in php when using the checkboxes in each row.

fortiz147
Newbie Poster
18 posts since Aug 2008
Reputation Points: 11
Solved Threads: 0
 

thank you!!!! i will try the code. =)

fortiz147
Newbie Poster
18 posts since Aug 2008
Reputation Points: 11
Solved Threads: 0
 

hi.. i am a also a newbie.. try this one.. this worked for me. :)

<html>
	<head>

	</head>
	<body>
		<?php
			//mysql connection
			$dbconn = mysql_connect("","","") or die();
			mysql_select_db("table") or die();
			
			$sqlquery = "your query"; // query on table
			$sqlresult = mysql_query($sqlquery, $dbconn);
			$count = mysql_num_rows($sqlresult); // count query result			
			
		?>
		
		<form method="post" action="delete.php">
			<!--
				some table headers here / or html tags
			-->
		<?php
			//loop here
			while($row = mysql_fetch_array($sqlresult))
			{
			//inside while
		?>
				<tr>
					<td><input type="checkbox" name="checkbox[]" id="checkbox[]"  value="<?php echo $row['id']?>" /></td>
					<!--
					and other data you want to print out..
					-->
				</tr>
		<?php	
			}
		?>	
				<tr><input id='delete' type='submit' name='delete' value='Delete'/></tr>
		</form>
	</body>
</html>

<?php
#these following code for delete.php file 
?>

<html>
	<?php
		//mysql connection here
		
		if($_POST['delete']) // from button name="delete"
		{
			$checkbox = $_POST['checkbox']; //from name="checkbox[]"
			$countCheck = count($_POST['checkbox']);
			
			for($i=0;$i<$countCheck;$i++)
			{
				$del_id  = $checkbox[$i];
				$sql = "delete from table where id = $del_id";
				$result = mysql_query($sql, $dbconn);
			}
				if($result)
				{
					echo "successful delete";
				}
				else
				{
					echo "Error: ".mysql_error();
				}
		}
	?>
</html>
enim213
Light Poster
40 posts since Jun 2008
Reputation Points: 11
Solved Threads: 2
 

finally!!!!!!! it worked! thank you to everyone especially to enim!

fortiz147
Newbie Poster
18 posts since Aug 2008
Reputation Points: 11
Solved Threads: 0
 

you're welcome.! i hope i could solve my prob. too.. hehehehe:)

enim213
Light Poster
40 posts since Jun 2008
Reputation Points: 11
Solved Threads: 2
 

Thak you very much!

guys i just added two small variables because register globals is been removed from new version of php this will work 100%

<?php
$host="localhost"; // Host name
$username="root"; // <strong class="highlight">Mysql</strong> username
$password=""; // <strong class="highlight">Mysql</strong> password
$db_name="mytest"; // Database name
$tbl_name="userreg"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if <strong class="highlight">delete</strong> button active, start this
$delete = $_POST[delete];
$checkbox = $_POST[checkbox];
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
ersind
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

nice code. workd for me enim213!

SKANK!!!!!
Posting Pro in Training
429 posts since Apr 2009
Reputation Points: 15
Solved Threads: 7
 
nice code. workd for me enim213!

thank. glad to help. :) - enim213

enim213
Light Poster
40 posts since Jun 2008
Reputation Points: 11
Solved Threads: 2
 

:) hey buddy enim,thanks. itz really a very good code and it workd for me too.
thanks again
god bless u

falguni dattani
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

but u solved my prob. so how can u say that u solved yr prob!!!

falguni dattani
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

i did too.. that was way back then.. :)

enim213
Light Poster
40 posts since Jun 2008
Reputation Points: 11
Solved Threads: 2
 

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\kregpho.php:2) in D:\xampp\htdocs\kregpho.php on line 72


can any 1 tell me wat this error mean....y should it occur.

kiranhg.2008
Newbie Poster
11 posts since Nov 2009
Reputation Points: 9
Solved Threads: 0
 

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\kregpho.php:2) in D:\xampp\htdocs\kregpho.php on line 72

can any 1 tell me wat this error mean....y should it occur.


It means that you use a header function(header(), setcookie(), session_start(), etc...) after you started sending content. After you start sending content, the headers can not be modified.

And by "sending content", I mean anything that would show up in the browsers "view source" output. Even a white-space before your opening <?php tags would cause this error.

P.S.
Why did you post this into an old, random thread?

Atli
Posting Pro
540 posts since May 2007
Reputation Points: 93
Solved Threads: 70
 

because everyone is subscribed to it and he knos it

SKANK!!!!!
Posting Pro in Training
429 posts since Apr 2009
Reputation Points: 15
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You