Change my stock status?

Good morning

How do I change the status of my stock so that when the quantity is 0 it indicates a red “out of stock” button?

Can you help me ?

Produit.php

<?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();
            echo "<script> window.location.href='produit.php'</script>";
        }

?>


<!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>
                                    
                                     <a href="updateProduit.php?id_produit=<?php echo $row->id_produit;?>"><button class="btn btn-primary" ><i class="fas fa-edit"></i></button></a>
                                    <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>

There needs to be some conditional that queries the stock quantity and changes the badge accordingly.
Though it is best to keep that kind of logic out of your HTML and do all the data processing before that.

On an unrelated side note, it’s not a good idea to have Delete actions on a GET request, use POST instead.

1 Like

Thank you for your response.

I understood the theory for the condition but to do it in practice how should I do it?

I’m a novice and I’m following an online tutorial on php/mysql

In this simplest form:-

if($row->quantite < 1){    // Quantity is less than 1.
    echo '<td > <span class="badge bg-fail">No Stock</span> </td>' ;
}
else{
    echo '<td > <span class="badge bg-success">en Stock</span> </td>' ;
}

Change the class and wording to suit.
Though this is not how I would do this. I would probably create a class for products with methods to handle setting up all this and do most of the logic processing outside (before) the HTML.
The logic may be the same, but instead of directly outputting to HTML I would store the result in an object property to be output later at the right place.
For more complex logical processing it can get very messy and you should keep that away from the HTML or it will be confusing and hard to manage.

1 Like

thank you

I am unable to do a test because when I try to register a new product and I indicate 0 in quantity it does not register it I am forced to indicate a quantity greater than 0

Do you have an idea ?

here is my inde.php code and my product code.php

index.php

<?php
        // faire appel a la base de donnees

        require_once 'db.php';
   
        // ajouter un produit depuis le formulaire 

if(isset($_POST['ajouter']) && !empty($_POST['code_article'])
                            && !empty($_POST['nom_article'])
                            && !empty($_POST['quantite'])

)
{   
    $code_article = $_POST['code_article'];
    $nom_article = $_POST['nom_article'];
    $quantite = $_POST['quantite'];

    // pour l'image ya bouceaup de chose a jouter 
    // je laisse le code dans un lien dans la descreption 

        $images=$_FILES['profile']['name'];
		$tmp_dir=$_FILES['profile']['tmp_name'];
		$imageSize=$_FILES['profile']['size'];
            // creer un dossier nommer le uplods 
            // pour stocker nos images
		$upload_dir='uploads/';
		$imgExt=strtolower(pathinfo($images,PATHINFO_EXTENSION));
		$valid_extensions=array('jpeg', 'jpg', 'png', 'gif', 'pdf');
		$picProfile=rand(1000, 1000000).".".$imgExt;
		move_uploaded_file($tmp_dir, $upload_dir.$picProfile);

        $sql ="INSERT INTO produit(code_article, nom_article, image_produit, quantite)
         VALUES (:code_article, :nom_article, :pic, :quantite)";
         $stmt = $pdo->prepare($sql);

         $stmt->bindParam(':code_article', $code_article);
         $stmt->bindParam(':nom_article', $nom_article);
         $stmt->bindParam(':pic', $picProfile);
         $stmt->bindParam(':quantite', $quantite);

         $stmt->execute();
         header('Location:index.php');
}
            $stmt = $pdo->query('SELECT * FROM produit');
?>

<!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>Gestion de Stock</title>
</head>
<body>
<?php require_once 'navbar.php'; ?>
   
    <div class="container">
        <div class="row">
            <div class="col-2 mt-3">
               <form action="index.php" class="form-group mt-3" method="POST" enctype="multipart/form-data" >
                <label for="">Image du Produit :</label>
                     <input type="file" class="form-control mt-3" name="profile" accept="*/image">
                   <label for="">Code Article :</label>
                   <input type="text" class="form-control mt-3" name="code_article" required>
                   <label for="">Designation du Produit :</label>
                   <input type="text" class="form-control mt-3" name="nom_article" required>
                   <label for="">Quantite :</label>
                   <input type="text" class="form-control mt-3" name="quantite" required>
                   <button type="submit" class="btn btn-primary mt-3" name="ajouter">Enregistrer</button>
               </form>
            </div>
            <div class="col-10 mt-3">
                <table class="table table-striped display" id="example">
                    <thead>
                        <th>Image</th>
                        <th>Code Article</th>
                        <th>Designation</th>
                        <th>Quantite</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>
                        </tr>
                       <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
     <script>
                $(document).ready(function() {
                    $('#example').DataTable( {
                        "scrollY":         "500px",
                        "scrollCollapse": true,
                        "paging":         false
                    });
                } );
            </script>    
            
</body>
</html>

produit.php

<?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();
            echo "<script> window.location.href='produit.php'</script>";
        }

?>


<!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>
                            <?php
                            if($row->quantite < 1){    // Quantity is less than 1.
                                echo '<td > <span class="badge bg-fail">No Stock</span> </td>' ;
                            }
                            else{
                                echo '<td > <span class="badge bg-success">en Stock</span> </td>' ;
                            }
                            ?>
                            <td>
                                    
                                     <a href="updateProduit.php?id_produit=<?php echo $row->id_produit;?>"><button class="btn btn-primary" ><i class="fas fa-edit"></i></button></a>
                                    <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>

The problem is here. 0 will be returned as an empty value.

1 Like

ah ok I have to remove this line?

finally empty I meant

Removing the line will allow a 0 value to be passed. It will also allow an empty value too, which you may not want. Though you could set it to default to 0 in the absence of any value.

1 Like

ok and if I want to define the quantity value to 0 I must do it at what level in the form? or here
$quantity = $_POST[‘quantity’]; ?

This will default to 0 if there is no value.

if(!$_POST['quantite']){ $quantite = 0;} // If false, default to 0
else{ $quantite = abs($_POST['quantite']);} // Absolute numeric value
1 Like

I arrived and it works, thank you very much for your help.

I also wanted to ask you how to limit entry to a maximum of 9 digits for Article Code?

I did it like this I think it’s good?

<input type="text" class="form-control mt-3" minlength='9' maxlength='9' name="code_article" required>

Just maxlength is enough, with minlength and maxlength set to 9 anything that isn’t exactly 9 characters long is invalid.

You need to trim user submitted data, mainly so that you can detect if a value is all white-space characters, then validate all input data separately. Form data are string datatypes, regardless of what value they are. You should test if an input is or is not exactly an empty string, using === ‘’ or !== ‘’

After a redirect, you need to have an exit/die statement to stop php code execution. Currently, the rest of your code still runs while the browser is requesting the URL.

1 Like