0

I'm attempting to delete a row from a table with data that is generated from a MySQL table. I took a look at both of these questions: 1. How to delete rows of database results using checkbox 2.Deleting multiple rows using checkboxes, PHP and MySQL.

I need help for code delete using checkboxes..

<?php

$sql = mysqli_query($conn, "SELECT * FROM leads ORDER BY lid ASC");
if(mysqli_num_rows($sql) == 0){
   echo '<tr><td colspan="8">No Data Entry</td></tr>';
}else{
    while($row = mysqli_fetch_assoc($sql)){
        echo '<tr>
                <td> <input name"checkbox[]" value"'.$row['lid'].'" type="checkbox"></td>
                <td>'.$row['name'].'</td>
                <td>'.$row['sex'].'</td>
                <td>'.$row['phone'].'</td>
                <td>'.$row['company'].'</td>
                <td>'.$row['vehicle'].'</td>
            </tr>';
        }
    }?>

Cancel

 <?php

   $del_lid = $_POST['checkbox']; 
   if (isset($_POST['submit'])) {
     foreach($del_lid as $value){
       $sql = "DELETE FROM leads WHERE lid='".$value."'";
       $result = mysqli_query($conn, $sql);
    }
 }
?>
4
  • Any kind of help or suggestions are appreciated. thank you.... Commented Jun 23, 2017 at 8:37
  • 3
    missing an equals sign from value"'.$row['lid'].'" Commented Jun 23, 2017 at 8:38
  • where your facing problem ? Commented Jun 23, 2017 at 8:38
  • thank you for response guys the true is i newbie in php coding @JYoThl i thing my problem for execute the delete statement. Commented Jun 23, 2017 at 8:46

1 Answer 1

2

The checkboxes were missing the equals sign so effectively none of them had a value. The sql could be streamlined to use the in operator rather than a loop.

    <table>
<?php

    $sql = mysqli_query($conn, "SELECT * FROM leads ORDER BY lid ASC");
    if( mysqli_num_rows($sql) == 0 ){
        echo '<tr><td colspan="8">No Data Entry</td></tr>';
    }else{
        while($row = mysqli_fetch_assoc($sql)){
            echo '
            <tr>
                <td> <input name="checkbox[]" value="'.$row['lid'].'" type="checkbox"></td>
                <td>'.$row['name'].'</td>
                <td>'.$row['sex'].'</td>
                <td>'.$row['phone'].'</td>
                <td>'.$row['company'].'</td>
                <td>'.$row['vehicle'].'</td>
            </tr>';
        }
    }
?>
        </table>
        <input type="submit" name="delete" class="btn btn-sm btn-primary" value="delete">
        <a href="index.php" class="btn btn-sm btn-danger">Cancel</a></td>
    </form>
</div>
<?php

    $del_lid = $_POST['checkbox']; 

    if ( isset($_POST['submit'] ) ) {
        $sql='delete from `leads` where `lid` in ( ' . implode( ',', $del_lid ). ' )';
        $result = mysqli_query($conn, $sql);
    }
?>
Sign up to request clarification or add additional context in comments.

11 Comments

How about using SQL injection protection: $del_lid = array_map('intval',$_POST['checkbox']);
True - SQL Injection is an issue - a prepared statement would be much better but the array_map function call is a good shout!
Hello Ram i still getting an error Notice: Undefined index: checkbox in C:\xampp\htdocs\mysqlicrud\leads.php on line 49 this line => $del_lid = $_POST['checkbox'];
<input name"checkbox[]" is missing the equals sign after name
thank you very much guys i got the answers i missed the if ( isset($_POST['submit'] ) ) suppose to be if ( isset($_POST['delete'] ) ) sorry my bad but thank you again for help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.