简体   繁体   English

如何使用PHP在数据库中搜索特定行?

[英]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? 如何从HTML页面中的表单进行搜索以在同一HTML页面中显示结果? Am having an error when i try and run the code that says inputPackageID is not defined. 当我尝试运行未定义inputPackageID的代码时出现错误。 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. 它向您显示未定义,因为您的表单使用了post方法,并且您使用的是GET数组。

$PackageID = $_GET['inputPackageID'];

change that to 更改为

$PackageID = $_POST['inputPackageID'];

Plus, use isset() 另外,使用isset()

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

since you're using your entired code inside the same page. 因为您在同一页面中使用整个代码。

or !empty() !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. 如果您在同一文件中使用相同的代码,则可以为您的提交按钮使用action=""和一个附加的条件语句。

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. 但是,如果真正要使用GET方法,则需要在表单中将方法更改为method="get" Using this method is unsafe, consult my footnotes. 使用此方法是不安全的,请查阅我的脚注。


Footnotes: 脚注:

Your present code is open to SQL injection . 您当前的代码可以进行SQL注入 Use mysqli with prepared statements , or PDO with prepared statements , they're much safer . mysqli与预处理语句配合使用,或将PDO与预处理语句配合使用则更加安全


"How can i make a search from the form in an HTML page display the result in the same html page?" “如何从HTML页面中的表单进行搜索以在同一HTML页面中显示结果?”

  • You can either use Ajax for this, or use action="" . 您可以为此使用Ajax,也可以使用action=""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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