good morning. i am new still new in php. I need help from you all. why i cannot delete
multiple rows from mysql with checkbox in my database... this is my coding..

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="cek"; // Database name
$tbl_name="del"; // 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");

// Connect to server and select databse.


$sql="SELECT * FROM del";
$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="delete.php">
<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; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete" action="delete.php"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM del WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo '<META HTTP-EQUIV="Refresh" CONTENT="0.01; URL=delete.php">';
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
all help from all of you I really appreciate. :)

Recommended Answers

All 7 Replies

check this:)

for($i=0;$i<$count;$i++){

where did you define $count?

good morning. i am new still new in php. I need help from you all. why i cannot delete
multiple rows from mysql with checkbox in my database... this is my coding..

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="cek"; // Database name
$tbl_name="del"; // 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");

// Connect to server and select databse.


$sql="SELECT * FROM del";
$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="delete.php">
<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; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete" action="delete.php"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM del WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo '<META HTTP-EQUIV="Refresh" CONTENT="0.01; URL=delete.php">';
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
all help from all of you I really appreciate. :)

Use code tags to post your code.

tq.. i will try it.. :)

hello Karthik_pranas and Ichcha
i still can't do it..
i already post a new thread..

hello... i need help from you all.. i am still new in php.. i hope you all can help me.. i am desperate. i can't delete delete data(delete multiple row from mysql using check box).
this is my coding.. the cooperation is greatly appreciated. i have 2 file
this is my coding

1 file = test1.php
<html>
<HEAD>
<?php session_start(); ?>
<?php

include("db_connect.php"); //connection
mysql_select_db($database_localhost, $conn)or trigger_error(mysql_error(),E_USER_ERROR);

$result=mysql_query("SELECT * FROM cek1a");
?>
<script language=javascript>
function validate()
{
var r=confirm("Adakah anda pasti ingin padam?")

if (r==true)
return true;
else
return false;
}

</script>
</head>

<table bordercolor="#CCCCCC" border='1' cellspacing='1' cellpadding='1'>

<tr>
<form name="test1" method="post" action="test2.php" onSubmit="return validate()">
<tr>
<th bgcolor="#6495ED">NoCek</TH>
<th bgcolor="#6495ED">NAMA </TH>
<th bgcolor="#6495ED">JUMLAH</TH>
<th bgcolor="#6495ED">TUJUAN</TH>
</TR>
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><?php echo $row;?></td>
<td align="center"><?php echo $row;?></td>
<td align="center"><?php echo $row;?></td>
<td align="center"><?php echo $row;?></td>
<td align="center"><input type="checkbox" name="delete[]" value="<?php echo $row;?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4">
<CENTER><input type="submit" value="HAPUS"></CENTER>

</td>
</tr>
</form>


</table>
</body>
</html>

second file = test2.php


<?php
include("db_connect.php");


if (isset($_POST) && is_array($_POST))
{
foreach ($_POST as $num => $value)
{
$sql="delete from cekla where noC='$value'";
$result=mysql_query($sql);
}
}
header("location:test1.php");
?>

i hope you all can help me.

if(isset($_POST['deleteme'])){

        $count = count($_POST['multiple']);
        $data = $_POST['multiple'];

        for($i=0;$i<$count;$i++){
            $id = $data[$i];
            mysql_query('delete from multiple where id='.$id);
        }
    }

<td><input type="checkbox" name="multiple[]" value="<?php echo $result['id']; ?>"/></td>

Try this

<?php
include("db_connect.php");
extract($_POST);

if (isset($delete))
{
for($i=0;$i<count($delete);$i++)
{
$sql="delete from cekla where noC='".$delete[$i]."'";
$result=mysql_query($sql);
}
}
header("location:test1.php");
?>

use code tags to post your code. It will helpful for us to understand your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.