mardi 30 mars 2021

php form not showing query data in all if statement

i have make a form whit 3 input, i have make a if statement where if there is input 1 it show the query result in a table if there is input 2 show the query result in the table and if there is input 3 show the query result in the table like. the form is working and show the result of the inpur 2 and input 3 but now show the result of the input 1.

code below:

    <?php include('header.php');?>
        
        <div class="row">
            <div class="col-md-4">
                <h1>Prodotti</h1>
            </div>
            <div class="col-md-8">
            <a href="aggiungi_prodotto.php" class="btn btn-primary btn-block">Nuovo Prodotto</a>
            </div>
        </div> 

        <form class="row" action="prodotti.php" method="post">
            <div class="col-md-12">
                <div class="row g-3 align-items-center mb-2">
                    <div class="col-sm-2">
                        <label for="descrizione" class="col-form-label">Descrizione:</label>
                    </div>
                    <div class="col-sm-10">
                        <input type="text" id="descrizione" class="form-control" name="descrizione">
                    </div>
                </div>
                <div class="row g-3 align-items-center mb-2">
                    <div class="col-sm-2">
                        <label for="codiceArt" class="col-form-label">Codice Prodotto:</label>
                    </div>
                    <div class="col-sm-10">
                        <input type="number" id="codiceArt" class="form-control" name="codice-prodotto">
                    </div>
                </div>
                <div class="row g-3 align-items-center mb-2">
                    <div class="col-sm-2">
                        <label for="inputBarcode" class="col-form-label">Barcode:</label>
                    </div>
                    <div class="col-sm-10">
                        <input type="number" id="inputBarcode" class="form-control" name="barcode">
                    </div>
                </div>
                <div class="row g-3 align-items-center mb-2">
                    <div class="col-sm-12">
                    <button type="submit" value="submit" class="btn btn-primary btn-block">Cerca</button>
                    </div>
                </div>
            </div>
        </form>
        
                <table class="table table-hover">
                    <thead>
                        <tr>
                        <th scope="col">Riga</th>
                        <th scope="col">Codice Articolo</th>
                        <th scope="col">Descrizione</th>
                        <th scope="col">Prezzo</th>
                        <th scope="col">Barcode</th>
                        <th scope="col">Data</th>
                        </tr>
                    </thead>
                    <tbody>
                    
                    <?php
                    if($_POST['descrizione']){
                        $descrizione = $_POST['descrizione'];
                        $sql = "SELECT * FROM prodotti WHERE descrizione = ". $descrizione;
                        $result = $mysqli->query($sql);
                        echo $result;
                        if($result->num_rows > 0){
                            $row_number = 0;
                            while ($row = mysqli_fetch_assoc($result)) {
                                $row_number ++;
                            ?>
                                <tr>
                                <th scope="row"><?php echo $row_number;?></th>
                                <td><?php echo $row['codice'];?></td>
                                <td><?php echo $row['descrizione'];?></td>
                                <td><?php echo $row['prezzo'];?></td>
                                <td><?php echo $row['barcode'];?></td>
                                <td><?php echo $row['data_creazione'];?></td>
                                </tr>
                            <?php
                            }
                        }


                    }elseif($_POST['codice-prodotto']){
                        $codice_prodotto = $_POST['codice-prodotto'];
                        $sql = "SELECT * FROM prodotti WHERE codice = ". $codice_prodotto;
                        $result = $mysqli->query($sql);
                        if($result->num_rows > 0){
                            $row_number = 0;
                            while ($row = mysqli_fetch_assoc($result)) {
                                $row_number ++;
                            ?>
                                <tr>
                                <th scope="row"><?php echo $row_number;?></th>
                                <td><?php echo $row['codice'];?></td>
                                <td><?php echo $row['descrizione'];?></td>
                                <td><?php echo $row['prezzo'];?></td>
                                <td><?php echo $row['barcode'];?></td>
                                <td><?php echo $row['data_creazione'];?></td>
                                </tr>
                            <?php
                            }
                        }


                    }elseif($_POST['barcode']){
                        $barcode = $_POST['barcode'];
                        $sql = "SELECT * FROM prodotti WHERE barcode = ". $barcode;
                        $result = $mysqli->query($sql);
                        if($result->num_rows > 0){
                            $row_number = 0;
                            while ($row = mysqli_fetch_assoc($result)) {
                                $row_number ++;
                            ?>
                                <tr>
                                <th scope="row"><?php echo $row_number;?></th>
                                <td><?php echo $row['codice'];?></td>
                                <td><?php echo $row['descrizione'];?></td>
                                <td><?php echo $row['prezzo'];?></td>
                                <td><?php echo $row['barcode'];?></td>
                                <td><?php echo $row['data_creazione'];?></td>
                                </tr>
                            <?php
                            }
                        }

                    }?>
                        
                        
                    </tbody>
                </table>
           
         


<?php include('footer.php');?>

I really don't understand why.

how can i solve the problem?

the database have a table named "prodotti" with the following colums "id, codice, descrizione, barcode, prezzo, data_creazione"

Aucun commentaire:

Enregistrer un commentaire