【发布时间】:2020-08-01 11:01:45
【问题描述】:
我的问题是我有一个带有搜索文本框的表格,我可以使用该文本框从该表格中获取特定数据,同时我有另一个文本框来获取货币列的总金额,我的问题是当我搜索特定数据,然后我尝试获取特定数据的总数,它给了我列中整个数据的总数,而不是过滤后的数据,我尝试同时使用查询,但我做不到,
This is a Screenshot for the form and the textboxes
注意我在这个例子中使用地址栏只是为了测试
这是带有按钮和表格的 Searching 文本框的 HTML with PHP First 代码:
<?php
if(isset($_POST['search'])){
$search_by_word = $_POST['search'];
$query = "SELECT * FROM test WHERE firstname LIKE '%$search_by_word%' ";
}else{
$query = "SELECT * FROM test";
}
$query_conn = mysqli_query($connection, $query);
?>
<form action="" method="POST">
<div class="col-md-6">
<input type="text" name="search" class="form-control" placeholder="Search">
</div>
<div class="col-md-6 text-left">
<button class="btn btn-primary">Search</button>
</div>
</form>
<br><br>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($query_conn)){
$user_id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$address = $row['address'];
$email = $row['email'];
echo "<tr>";
echo "<td>$user_id</td>";
echo "<td>$firstname</td>";
echo "<td>$lastname</td>";
echo "<td>$address</td>";
echo "<td>$email</td>";
echo "</tr>";
}
?>
</tbody>
</table>
这是我的第二个代码Total:
<?php
if(isset($_POST['submit'])){
$query = "SELECT sum(address) AS totalofmoney FROM test";
$query_conn = mysqli_query($connection, $query);
$data = mysqli_fetch_assoc($query_conn);
$total = "Total of Salary: " . $data['totalofmoney'];
}else{
$total = "Total of Salary: ";
}
?>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
echo "<tr>";
echo "<td>$total</td>";
echo "</tr>";
?>
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<form action="search_table3.php" method="POST">
<div class="col-md-6 text-left">
<button name="submit" type="submit" class="btn btn-primary">Sum</button>
<a href="" class="btn btn-primary">Print</a>
</div>
</form>
</div>
</div>
提前感谢您的帮助。
【问题讨论】:
-
您不能尝试获取页面加载时的所有记录,然后仅过滤该列表而不是每次搜索时都获取记录吗?
-
嗨@JasperB,我这样做了,但它会给我该列中全部数据的总和,我想得到特定数据的总和,因为如你所见,它们是两个查询代码,所以它会在得到总和之前重新加载数据,这是我的问题。