0

I am using following code to delete multiple vlues from database, but some how i have to click the delete button twice to delete the values, Please look through the following code and suggest me how to delete values with single click

<?php

include("connection.php");
$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if (isset($_POST['delete']))
{
    if (isset($_POST['checkbox']))
    {
        $checkbox = $_POST['checkbox'];
        if (is_array($checkbox)) {

            foreach ($checkbox as $key => $id)
            {
                mysql_query("DELETE FROM deptag WHERE id=".$id); 
                }
            }

    }
}
?>
<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>Tag</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['tagdep']; ?></td>

</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

</table>
</form>
</td>
</tr>
</table>
zuber
  • 3
  • 2
  • 6
  • People have already answered your question, but I highly suggest you stop using mysql_* functions, they have been deprecated for mysqli_* - while you are refactoring, I would strongly suggest you update your database connectivity methods – Trent Mar 24 '14 at 05:57

4 Answers4

0

just move your select query below delete code or use this

<?php
include("connection.php");
if (isset($_POST['delete']))
{
    if (isset($_POST['checkbox']))
    {
        $checkbox = $_POST['checkbox'];
        if (is_array($checkbox)) {

            foreach ($checkbox as $key => $id)
            {
                mysql_query("DELETE FROM deptag WHERE id=".$id); 
                }
            }

    }
}
$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
PravinS
  • 2,640
  • 3
  • 21
  • 25
0

You need to move the following code to immediately after the include("connection.php"); line

if(isset($_POST['delete']))
{
    if(isset($_POST['checkbox']))
    {
        $checkbox = $_POST['checkbox'];
        if (is_array($checkbox)) {
            foreach ($checkbox as $key => $id)
            {
                mysql_query("DELETE FROM deptag WHERE id=".$id); 
            }
        }
    }
}

After this place the code

$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

Note: mysql_* functions are deprecated, so that, use mysqli_* or PDO

Tun Zarni Kyaw
  • 2,099
  • 2
  • 21
  • 27
0

You have to rearrange you code so that it will fetch data from db after the delete

if (isset($_POST['delete']))
{
    if (isset($_POST['checkbox']))
    {
        $checkbox = $_POST['checkbox'];
        if (is_array($checkbox)) {

            foreach ($checkbox as $key => $id)
            {
                mysql_query("DELETE FROM deptag WHERE id=".$id); 
                }
            }

    }
}

$sql="SELECT * FROM deptag";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

PS : Please consider sql injection vulnerability

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Nauphal
  • 6,194
  • 4
  • 27
  • 43
0

I'll convert first your MySQL to MySQLi:

connection.php:

<?php

$con=mysqli_connect("localhost","YourUsername","YourPassword","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

?>

YOUR DELETE FILE:

<?php

include("connection.php");

if (isset($_POST['delete']))
{
$counter=$_POST['hiddencounter'];
$checkbox=$_POST['checkbox'];

for($x=0;$x<=$counter;$x++){

if(empty($checkbox[$x])){
/* DO NOTHING */
}

else {
mysqli_query($con,"DELETE FROM deptag WHERE id='checkbox[$x]'");
}

} /* END OF FOR LOOP */

} /* END OF IF ISSET DELETE */

$result=mysqli_query($con,"SELECT * FROM deptag");
$count=mysqli_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>Tag</strong></td>

</tr>
<?php

$counter=0; /*    LET US SET A COUNTER     */

while($rows=mysqli_fetch_array($result)){
$id=$rows['id'];
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo "<input name='checkbox[$counter]' type='checkbox' id='checkbox' value='$id'>"; ?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['tagdep']; ?></td>

</tr>
<?php
$counter=$counter+1; /*    INCREMENT COUNTER EVERY LOOP    */
} /*    END OF WHILE LOOP    */
echo "<input type='hidden' name='hiddencounter' value='$counter'>"; /* PUT THE OVERALL COUNTER INTO A HIDDEN FIELD */
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

</table>
</form>
</td>
</tr>
</table>
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49