vendredi 23 juin 2017

delete from MySQL database using checkboxes

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: How to delete rows of database results using checkbox and 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>
                    ';
                }
                }
            ?>
        </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'])) {
        foreach($del_lid as $value){
       $sql = "DELETE FROM leads WHERE lid='".$value."'";
       $result = mysqli_query($conn, $sql);
    }
    }


    ?>




Aucun commentaire:

Enregistrer un commentaire