![](/img/trans.png)
[英]Search for specific row in database and remove matching row in CSV in PHP
[英]How can i search for a specific row in a Database using PHP?
如何从HTML页面中的表单进行搜索以在同一HTML页面中显示结果? 当我尝试运行未定义inputPackageID的代码时出现错误。 我该如何解决这个问题。 似乎无法从表单中识别变量。 请帮忙
这是我的代码。 如果您不理解我的问题,可以问我不清楚的地方。
<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>
它向您显示未定义,因为您的表单使用了post方法,并且您使用的是GET数组。
$PackageID = $_GET['inputPackageID'];
更改为
$PackageID = $_POST['inputPackageID'];
另外,使用isset()
if(isset($_POST['inputPackageID'])){
$PackageID = $_POST['inputPackageID'];
}
因为您在同一页面中使用整个代码。
或!empty()
if(!empty($_POST['inputPackageID'])){
$PackageID = $_POST['inputPackageID'];
}
如果您在同一文件中使用相同的代码,则可以为您的提交按钮使用action=""
和一个附加的条件语句。
if(isset($_POST['submit'])){...}
看到您的评论后,情况就不一样了,您使用的是单独的文件,因此我们可以进行抓取 。
但是,如果真正要使用GET方法,则需要在表单中将方法更改为method="get"
。 使用此方法是不安全的,请查阅我的脚注。
脚注:
您当前的代码可以进行SQL注入 。 将mysqli
与预处理语句配合使用,或将PDO与预处理语句配合使用 , 则更加安全 。
“如何从HTML页面中的表单进行搜索以在同一HTML页面中显示结果?”
action=""
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.