搜索按钮不显示表格上的特定数据

时间:2015-10-29 09:37:08

标签: php html mysql pdo

表格中有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='&diams; SAVE &diams;' 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

1 个答案:

答案 0 :(得分:0)

尝试添加检查以仅在未设置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='&diams; SAVE &diams;' 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>