简体   繁体   中英

How can i search for a specific row in a Database using PHP?

How can i make a search from the form in an HTML page display the result in the same html page? Am having an error when i try and run the code that says inputPackageID is not defined. How can i get to solve this. Seems like the variable is not being recognized from the form. Please help

Here is my code. If you dont understand my question you can ask me where you are not clear.

<div class="main">
    <div class="col-md-12">
        <div class="row">
            <div class="container">

        </div>

        <div class="search center-block col-md-6" align="center">
        <p>
            Want to track your package? <br />
            Enter your Package ID(PID)
        </p>
        <form method="post" action="trackShipment.php">
        <div class="form-group">

            <label class="sr-only" for="inputPackageID"></label>
            <input type="search" name="inputPackageID" class="form-control edit-form-control" id="inputPackageID" placeholder="PID">
        </div>
        <div class="modal-footer">
          <input type="submit" class="btn btn-primary" name="submit" />
        </div>
        </form>
    </div>


        <div class="col-md-8" align="center">
        <h2>Well, Here is your package!</h2>
                    <?php
                        include '../includes/connection.php';
                        //include 'phpscripts/trackShipment.php';
                        $PackageID = $_GET['inputPackageID'];

                        $sql = "select * from package where p_id='$PackageID'";

?>






            <table class="table table-striped">
                <tr>
                    <th><span>PID</span></th>
                    <th><span>Customer ID</span></th>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Destination Per KM</th>
                    <th>Type</th>
                    <th>Price/KG</th>
                </tr>
                </th>
                <?php 

                     foreach ($db->query($sql) as $count)?>

                   <tr>
                        <td>
                            <?php echo $count['p_ID'];  ?>
                        </td>
                        <td>
                            <?php echo $count['cus_ID']; ?>
                        </td>
                        <td>
                            <?php echo $count['package_name']; ?>
                        </td>
                        <td>
                            <?php echo $count['package_description']; ?>
                        </td>
                        <td>
                            <?php echo $count['package_type']; ?>
                        </td>
                        <td>
                            <?php echo $count['destination_per_km']; ?>
                        </td>
                        <td>
                            <?php echo $count['price_per_kg']; ?>
                        </td>
                <?php } ?>
            </table>


    </div>
</div>

It's showing you that it's not defined because your form uses a post method, and you're using a GET array.

$PackageID = $_GET['inputPackageID'];

change that to

$PackageID = $_POST['inputPackageID'];

Plus, use isset()

if(isset($_POST['inputPackageID'])){
    $PackageID = $_POST['inputPackageID'];
}

since you're using your entired code inside the same page.

or !empty()

if(!empty($_POST['inputPackageID'])){
    $PackageID = $_POST['inputPackageID'];
}

If you're using the same code inside the same file, you can use action="" and an extra conditional statement for your submit button.

if(isset($_POST['submit'])){...}

Seeing a comment of yours, this isn't the case then and you are using a seperate file, so we can scratch that .

However, if a GET method is what you really want to use, you will need to change your method to method="get" in your form. Using this method is unsafe, consult my footnotes.


Footnotes:

Your present code is open to SQL injection . Use mysqli with prepared statements , or PDO with prepared statements , they're much safer .


"How can i make a search from the form in an HTML page display the result in the same html page?"

  • You can either use Ajax for this, or use action="" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM