Problem deleting an article

Good morning

I don’t understand why I have to click twice on the delete icon for a product to delete it? (I click the first time and it does nothing and then I click delete again and it deletes it.

Do you have an idea ?

<?php 
        require_once 'db.php';

        $stmt = $pdo->query('SELECT * FROM produit');

        if(isset($_REQUEST['del']))
        {
            $sup = intval ($_GET['del']);

            $sql = "DELETE FROM produit WHERE id_produit=:id_produit";
            $query = $pdo->prepare($sql);
            $query->bindParam(':id_produit', $sup , PDO::PARAM_STR);
            $query->execute();
        }

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php require_once 'navbar.php'; ?>
            <div class="container">
                <table class="table table-striped display mt-3" id="example">
                    <thead>
                        <th>image</th>
                        <th>Code Article</th>
                        <th>Designation</th>
                        <th>Quantite</th>
                        <th>Etat en stock</th>
                        <th>Action</th>
                    </thead>
                    <tbody>
                    <?php 
                    while ( $row =  $stmt->fetch())
                    {
                        ?>
                    
                        <tr>
                            <td><img src="./uploads/<?php echo $row-> image_produit; ?>" alt=" "  class="image_product "></td>
                            <td><?php echo $row-> code_article; ?> </td>
                            <td><?php echo $row-> nom_article; ?> </td>
                            <td><?php echo $row-> quantite; ?> </td>
                            <td > <span class="badge bg-success">en Stock</span> </td>
                            <td>
                                    
                                    <button class="btn btn-primary" ><i class="fas fa-edit"></i></button>
                                    <a href="produit.php?del=<?php echo $row->id_produit;?>"><button class="btn btn-danger" OnClick="return confirm ('Voulez vous vraiment supprimer')"><i class="fas fa-trash"></i></button></a>
                            
                            </td>
                        </tr>
                       <?php } ?>
                       
                    </tbody>
             </table>
            </div>

            <script>
                $(document).ready(function() {
                    $('#example').DataTable( {
                        "scrollY":         "500px",
                        "scrollCollapse": true,
                        "paging":         false
                    });
                } );
            </script>    
            
</body>
</html>
1 Like

Your code is out of order. Because you are running the SELECT query first, the display shows all the data, even if a row gets deleted.

You need to organize the code on a page in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce the data needed to display the page
  4. html document

The SELECT query needs to be part of item #3 on this list.

Next, you need to use a post method form when performing an action on the server, such as deleting data.

At the successful completion of your post method form processing code, you need to perform a redirect to the exact same URL of the current page to cause a get request for that page. This will prevent the browser from trying to resubmit the form data should that page get reloaded or browsed back to.

1 Like

Thank you for your detailed response.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.