搜索按钮未在表格上显示特定数据
Posted
技术标签:
【中文标题】搜索按钮未在表格上显示特定数据【英文标题】:Search button not displaying specific data on the table 【发布时间】:2016-01-29 09:03:02 【问题描述】:表中有 4 行数据。我设法使用 PDO 选择查询显示所有数据。我还有一个搜索按钮,用于显示我的数据库中的特定数据。它正在工作,但不是过滤和显示我搜索过的特定数据,它只是添加到表格中。 这是我的搜索按钮代码:
if(isset($_POST['search_btn']))
$row = null;
if(empty($_POST['InvNumbTxt']))
echo '<h3>*Please input invoice number!</h3>';
else
$invtxt = $_POST['InvNumbTxt'];
$dbh=db_connect();
$sql1="SELECT * FROM invalid_invoice WHERE Invoice_Number='".$invtxt."'";
$stmt1=$dbh->prepare($sql1);
$stmt1->execute();
while( $row2 = $stmt1->fetch(PDO::FETCH_ASSOC) ) ?>
<td><?php echo $row2['Issue_Type']; ?></td>
<td><?php echo $row2['Creation_Date']; ?></td>
<td><?php echo $row2['Site']; ?></td>
<td><?php echo $row2['Vendor_Name']; ?></td>
<td><?php echo $row2['Invoice_Date']; ?></td>
<td><?php echo $row2['Invoice_Number']; ?></td>
<td><?php echo $row2['Part_Number']; ?></td>
<td><?php echo $row2['PO']; ?></td>
<td><?php echo $row2['RR']; ?></td>
<td><?php echo $row2['Currency']; ?></td>
<td><?php echo $row2['Invoice_Amount']; ?></td>
<td><?php echo $row2['Issues']; ?></td>
<td><?php echo $row2['PersoninCharge']; ?></td>
<td><?php echo $row2['PIC_Comments']; ?></td>
<td><?php echo $row2['Status']; ?></td>
<td><?php echo $row2['ID']; ?></td>
<?php
?>
</tr>
<?php
$dbh = null;
?>
这是在表格上显示数据的代码:
$invtxt = $_POST['InvNumbTxt'];
$dbh=db_connect();
$sql="SELECT * FROM invalid_invoice";
$stmt=$dbh->prepare($sql);
$stmt->execute();
echo "<form name='update' method='POST'><tr class=output2>";
while($row =$stmt->fetch(PDO::FETCH_ASSOC))
echo "<td>".$row['Issue_Type']."</td>";
echo "<td>".$row['Creation_Date']."</td>";
echo "<td>".$row['Site']."</td>";
echo "<td>".$row['Vendor_Name']."</td>";
echo "<td>".$row['Invoice_Date']."</td>";
echo "<td>".$row['Invoice_Number']."</td>";
echo "<td>".$row['Part_Number']."</td>";
echo "<td>".$row['PO']."</td>";
echo "<td>".$row['RR']."</td>";
echo "<td>".$row['Currency']."</td>";
echo "<td>".$row['Invoice_Amount']."</td>";
echo "<td>".$row['Issues']."</td>";
echo "<td><input type='text' name='pic' value='".$row['PersoninCharge']."'></td>";
echo "<td><input type='text' name='comt' value='".$row['PIC_Comments']."'></td>";
echo "<td><input type='text' name='stat' value='".$row['Status']."'></td>";
echo "<td><input type='submit' name='save_btn' value='♦ SAVE ♦' style='font-size:1em;'/></td>";
echo "<td><input type='hidden' name='idtxt' value='".$row['ID']."'/></td>";
echo "</tr>";
echo "</form>";
$dbh=null;
<tr class=output2>
<?php
【问题讨论】:
您必须包含要获取所有行的代码 我已经在我的问题中添加了该部分供您参考 你能举一个没有搜索的行的例子吗? 默认情况下,所有数据都显示在表格上。然后我有一个用于搜索特定数据的搜索按钮。每当我搜索 invoice_number 时,数据都会添加到表格中,使其成为表格的新行。我想根据我搜索的内容在表格上显示特定数据 你有什么错误吗?? 【参考方案1】:尝试添加检查以仅在未设置 serach_btn
时显示所有记录。比如:
$invtxt = $_POST['InvNumbTxt'];
$dbh=db_connect();
$sql="SELECT * FROM invalid_invoice";
$stmt=$dbh->prepare($sql);
$stmt->execute();
echo "<form name='update' method='POST'><tr class=output2>";
if(!isset($_POST['search_btn']))
while($row =$stmt->fetch(PDO::FETCH_ASSOC))
echo "<td>".$row['Issue_Type']."</td>";
echo "<td>".$row['Creation_Date']."</td>";
echo "<td>".$row['Site']."</td>";
echo "<td>".$row['Vendor_Name']."</td>";
echo "<td>".$row['Invoice_Date']."</td>";
echo "<td>".$row['Invoice_Number']."</td>";
echo "<td>".$row['Part_Number']."</td>";
echo "<td>".$row['PO']."</td>";
echo "<td>".$row['RR']."</td>";
echo "<td>".$row['Currency']."</td>";
echo "<td>".$row['Invoice_Amount']."</td>";
echo "<td>".$row['Issues']."</td>";
echo "<td><input type='text' name='pic' value='".$row['PersoninCharge']."'></td>";
echo "<td><input type='text' name='comt' value='".$row['PIC_Comments']."'></td>";
echo "<td><input type='text' name='stat' value='".$row['Status']."'></td>";
echo "<td><input type='submit' name='save_btn' value='♦ SAVE ♦' style='font-size:1em;'/></td>";
echo "<td><input type='hidden' name='idtxt' value='".$row['ID']."'/></td>";
echo "</tr>";
echo "</form>";
$dbh=null;
<tr class=output2>
【讨论】:
以上是关于搜索按钮未在表格上显示特定数据的主要内容,如果未能解决你的问题,请参考以下文章