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

Delete multiple rows in mysql with check box

hi all. I have got below code from a website. Its work all but its can't delete the selected multiple row. plz help me... where is the problem.

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // 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 test_mysql";
$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, MYSQL_ASSOC)){

?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
// Check if delete button active, start this
if($delete){

for($i=0;$i<5;$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=dele.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
pranto157
Newbie Poster
20 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

here is the Database--

CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

pranto157
Newbie Poster
20 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // 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 test_mysql";
$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, MYSQL_ASSOC)){

?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
// Check if delete button active, start this
if($_POST['delete']){
//print_r($_POST);
//exit;
for($i=0;$i<count($_POST['checkbox']);$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=h_delete.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>


This should work.

Cheers,
Nav

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

sorry . this code does not work...
i think the value of checkbox doesnot passing...
there is the problem...
i think the checkbox value is null
i try this code by giving
DELETE FROM $tbl_name WHERE id=1
and its work

pranto157
Newbie Poster
20 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

It works for me though! But anyway, try the following.

$checkbox=$_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
.......
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

no its does not work
$del_id = $checkbox[$i];
i try to print print or echo $del_id
but it does not returns any value....
plz help nav33n

pranto157
Newbie Poster
20 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

hmm.. can you show your latest script ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum";
$tbl_name="test_mysql";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM test_mysql";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>

 
Delete multiple rows in mysql #
Id
Name
Lastname
Email
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>


<?php echo $rows['id']; ?>
<?php echo $rows['name']; ?><?php echo $rows['lastname']; ?>
<?php echo $rows['email']; ?>

<?php } ?>


<?php
if($_POST['delete']){
for($i=0;$i";}}
mysql_close();?>

pranto157
Newbie Poster
20 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test";
$tbl_name="users";
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, MYSQL_ASSOC)){ ?>

<tr><td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['user_id']; ?>">
</td><td bgcolor="#FFFFFF"><?php echo $rows['user_id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['login_name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['first_name']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
if($_POST['delete']){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
print $sql;
$result = mysql_query($sql);}

if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";}}
mysql_close();
}
?>
</table></form></td></tr></table>


This works for me. Umm.. I have also printed the query and it prints , DELETE FROM users WHERE id='1'
DELETE FROM users WHERE id='2'
DELETE FROM users WHERE id='3'

I have no clue why your code is 'losing' checkbox array values.


Try taking out this part if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";} and then check. Maybe the page is getting refreshed, so its losing the posted values. Lets see if that works.

Cheers,
Nav

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

i use php ----
Apache version :Apache/2.0.55 (Win32) PHP version :5.1.2 Loaded extensions :
bcmath, calendar, com_dotnet, ctype, date, ftp, iconv, odbc, pcre, Reflection, session, libxml, standard, tokenizer, zlib, SimpleXML, dom, SPL, wddx, xml, xmlreader, xmlwriter, apache2handler, mbstring, mysql, mysqli, PDO, pdo_sqlite, SQLite MySQL version :5.0.18-nt

this the problem... i want to say is the problem of php version....
or do i need any change in my php.ini file

pranto157
Newbie Poster
20 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Nope. There is nothing wrong with the php version you are using. Did you take off the refresh part of your code and tried again ? I would suggest you to split your code into small parts and make those small parts work first.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Here is the solution to your problem.

All your problems lie in the if statement, without nav's idea I wouldnt have figured out to solve this as I have been trying to figure it out for days but once I saw nav mention the $_POST it was all downhill. The main problem was getting the checkbox array value into the actual sql query, this has been done. Here is the solution, just alter the if statement and leave the rest of the code the same and this works. Enjoy X =)

if($_POST['delete'])
{
$checkboxID = $_POST['checkbox'];
for($i=0;$i<count;$i++) {
$del_id = $checkboxID[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);}
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

hi all. I have got below code from a website. Its work all but its can't delete the selected multiple row. plz help me... where is the problem.

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // 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 test_mysql";
$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, MYSQL_ASSOC)){

?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
// Check if delete button active, start this
if($delete){

for($i=0;$i<5;$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=dele.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>


Hi! Still anyone seeking the solution of this problem? Please give a knock on [email]rkarim1981@gmail.com[/email].

rkarim1981
Newbie Poster
4 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

i have this error:

Notice: Undefined index: delete in C:\Server\Apache2\htdocs\delete_multiple.php on line 60

TQ

riniuia
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // 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 test_mysql";
$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, MYSQL_ASSOC)){

?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php
// Check if delete button active, start this
if($_POST['delete']){
//print_r($_POST);
//exit;
for($i=0;$i<count($_POST['checkbox']);$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=h_delete.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

This should work.

Cheers, Nav


i have this error

Notice: Undefined index: delete in C:\Server\Apache2\htdocs\delete_multiple.php on line 60

line 60 refer to:

// Check if delete button active, start this
      if($_POST['delete']){ ---------this is line 60
      //print_r($_POST);
      //exit;
      for($i=0;$i<count($_POST['checkbox']);$i++){
       
      $del_id = $checkbox[$i];
       
      $sql = "DELETE FROM $tbl_name WHERE id=1";
      $result = mysql_query($sql);
      }
       
      // if successful redirect to delete_multiple.php
      if($result){
      echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
      }
      }

TQ

riniuia
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
rkarim1981
Newbie Poster
4 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

I ve modified this code try it it works 100% with php5

<body> 
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="empires"; // 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="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php  echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php 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>
<?php

$checkbox = $_POST['checkbox'];
$delete = $_POST['delete'];

// 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.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
honmugic
Newbie Poster
1 post since Sep 2009
Reputation Points: 10
Solved Threads: 0
 


<?php
echo "";
echo "  namepositionlevel";
while ($result = mysql_fetch_array($sql))
{
echo "$result[name] $result[position] $result[level]";
}

?>

jigneshpa
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

The error is line 37. You haven't defined the $checkbox variable.
Either add a new line before that with

$checkbox = $_POST['checkbox'];

or just replace line 37 with

$del_id = $_POST['checkbox'][$i];


-- EDIT --

never occured to check if there was a second page to the thread, so didn't see that you've already solved it ;)

Tompa07
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You