0

I am using the code (pasted below) to delete the record from the table when it's selected by the checkbox. It is working, but it is not deleting the record from the table, it is only showing the echo "Records deleted Successfully.".

Please have a look at my code and suggest me some changes.

<?php
echo "Hiiiiiiiiii";
include("conn.php");
$sql="select * from test ";

$res=mysql_query($sql) or die(mysql_error());
?>

<form name="form1" method="POST" action="">
    <table width="578" border="1" align="center" id="menu">
    <tr>
    <th></th>
    <th>id</th>
    <th>Name</th>
    <th>email</th>
    <th>phno</th>
 </tr>

<?php
 while($row=mysql_fetch_array($res))
 {
?>

 <tr>

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

    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['emailid'];?></td>

    <td><?php echo $row['phno'];?></td>
    <?php
    echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
    ?>
 <?php
  }
 ?>  
 <tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>

 <?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";

if(isset($_POST['delete']))
    {
         $delete_id = $_POST['checkbox'];
         $id = count($delete_id );
         if (count($id) > 0)
          {
             foreach ($delete_id as $id_d)
             {
                $sql = "DELETE FROM `test` WHERE id='$id_d'";
                $delete = mysql_query($sql);
            }
        }
        if($delete)
        {
            echo $id." Records deleted Successfully.";
        }
    }
>?
Jelmer
  • 2,663
  • 2
  • 27
  • 45
swapnil
  • 323
  • 2
  • 6
  • 13

8 Answers8

2

Add error_reporting(E_ALL); to the top of your file. Now you will be able to see all errors. http://php.net/manual/en/function.error-reporting.php

Also, use var_dump() to check what is actually in your variables. When you run var_dump($_POST); you can clearly see what is actually in your post. http://php.net/manual/en/function.var-dump.php

Jelmer
  • 2,663
  • 2
  • 27
  • 45
1
if(isset($_POST['delete'])){
    $delete = false;
    $ids = array();
    foreach($_POST['checkbox'] as $val){
        $ids[] = (int) $val;
    }
    $ids = implode("','", $ids);
    $sql = "DELETE FROM `test` WHERE id IN ('".$ids."')";
    $delete = mysql_query($sql);
    $id = mysql_affected_rows();
    if($delete){
        echo $id." Records deleted Successfully.";
    }
}

also your check-boxes should have:

<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>">
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

Put the delete code before selecting the records, then only you can see the actual records available in the DB.

    <?php
    echo "Hiiiiiiiiii";
    include("conn.php");

    if(isset($_POST['delete']))
        {
             $delete_id = $_POST['checkbox'];
             $id = count($delete_id );
             if (count($id) > 0)
              {
                 foreach ($delete_id as $id_d)
                 {
                    $sql = "DELETE FROM `test` WHERE id='$id_d'";
                    $delete = mysql_query($sql);
                }
            }
            if($delete)
            {
                echo $id." Records deleted Successfully.";
            }
        }


    $sql="select * from test ";

    $res=mysql_query($sql) or die(mysql_error());
    ?>
    <form name="form1" method="POST" action="">
        <table width="578" border="1" align="center" id="menu">
        <tr>
        <th></th>
        <th>id</th>
        <th>Name</th>
        <th>email</th>
        <th>phno</th>
     </tr>

<?php
 while($row=mysql_fetch_array($res))
 {
?>

 <tr>

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

    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['emailid'];?></td>

    <td><?php echo $row['phno'];?></td>
    <?php
    echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
    ?>
 <?php
  }
 ?> 
 <tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>

 <?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
>?
Arun David
  • 2,714
  • 3
  • 18
  • 18
0

There is a Typo in the code that outputs the id:

... value="<? echo $rows['id']; ?>">

It should be

... value="<? echo $row['id']; ?>">

Note that your code is also vulnerable to SQL-injection attacks. You should check if the ids in the POST-request are really integers.

Community
  • 1
  • 1
Sven Koschnicke
  • 6,523
  • 2
  • 34
  • 49
0
<? echo $rows['id']; ?>

there is no $rows here remove the s put

<? echo $row['id']; ?>
Ghassan Elias
  • 2,213
  • 1
  • 14
  • 17
0

You should name the checkboxes something like

name="checkbox[<?php echo $row['id'] ?>]"

And then when you're trying to delete

foreach ($_POST['checkbox'] as $id => $delete) {
    // delete id
}

Your way of selecting an id by count is rather awkward, you're also only deleting 1 row, which is apparently non-existant because the count is not an id. Also, the value of a checkbox is meant to be an indicator off the checkbox being ticked or not, it's not usually holding a value, the usual way is to make the name hold what the checkbox actually means.

fd8s0
  • 1,897
  • 1
  • 15
  • 29
0

you should move this code below of include("conn.php"); in your code

if (isset($_POST['delete'])) {
    $delete_id = $_POST['checkbox'];
    $id        = count($delete_id);
    if (count($id) > 0) {
        foreach ($delete_id as $id_d) {
            $sql    = "DELETE FROM `test` WHERE id='$id_d'";
            $delete = mysql_query($sql);
        }
    }
    if ($delete) {
        echo $id . " Records deleted Successfully.";
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Afshin
  • 4,197
  • 3
  • 25
  • 34
  • @afshim: Please make yourself comfortable with the formatting tools we have on this website. For example, I edited your answer to show that there is a formatting for muli-linie code fragments, too. I hope this helps you a bit. – hakre Sep 19 '12 at 10:21
0
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id'];?>"></td>
<td><?php echo $row['id'];?></td>

Need to change as below to work it perfectly:

<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="**<?php** echo $rows['id']; ?>"></td>
Nunser
  • 4,512
  • 8
  • 25
  • 37