我在一个表单中有两个选择框。过滤掉从数据库输出的选定值。这里是我的代码
html代码
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="width:100px; height:30px;">
<input type="date" name="date" id="date_id" style="width:150px; height:30px; margin-top:-29px; margin-bottom:0px; margin-left:0px;">
<select name="value1" style="width:51px; height:30px;margin-left:161px; margin-top:-29px; margin-bottom:0px;">
<option value="asc">ASC</option>
<option value="dsc">DSC</option>
</select>
<select name="value2" style="width:149px; height:30px;margin-left:226px; margin-top:-26px;">
<option value="readytoship">Ready To Ship</option>
<option value="readytodispatch">Ready To Dispatch</option>
</select>
<input type="submit" value="Filter" name="submit" style="margin-left:163px; margin-top:20px; margin-bottom:0px;"/>
</form>
php代码:
<?php
include "db.php";
if(isset($_POST['submit']))
{
if(($_POST['value1'] == 'asc') && ($_POST['value2'] == 'readytoship'))
{
// query to get all Fitzgerald records
$query = "SELECT * FROM purna_orders WHERE shipping_status='readytoship' order by po_created_date asc";
}
elseif(($_POST['value1'] == 'dsc') && ($_POST['value2'] == 'readytoship'))
{
// query to get all Herring records
$query = "SELECT * FROM purna_orders WHERE shipping_status='readytoship' order by po_created_date dsc";
}
elseif(($_POST['value1'] == 'asc') && ($_POST['value2'] == 'readytodispatch'))
{
// query to get all Herring records
$query = "SELECT * FROM purna_orders WHERE shipping_status='readytodispatch' order by po_created_date asc";
}
elseif(($_POST['value1'] == 'dsc') && ($_POST['value2'] == 'readytodispatch'))
{
// query to get all Herring records
$query = "SELECT * FROM purna_orders WHERE shipping_status='readytodispatch' order by po_created_date dsc";
}
else {
// query to get all records
$query = "select * from purna_orders where shipping_status='readytoship' order by po_created_date asc ";
}
$query = "select * from purna_orders where shipping_status='readytoship' order by po_created_date asc ";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if($num_rows >= 1)
{
echo "<div id='showmenu' class='scroll'>";
echo "<table id='table_struct' cellspacing='0' cellpadding='1' border='1' width='400' height='30'>
<tr class='tr_class' bgcolor='white'>
<td align='center' style='font-weight:bold'> Select </td>
<td align='center' style='font-weight:bold'> Order Id </td>
<td align='center' style='font-weight:bold'> Customer Name </td>
<td align='center' style='font-weight:bold'> Price </td>
<td align='center' style='font-weight:bold'> Pincode </td>
<td align='center' style='font-weight:bold'> COD </td>
<td align='center' style='font-weight:bold'> Status </td>
</tr>";
while($row = mysql_fetch_array($result))
{
$po_id = $row['po_id'];
$_SESSION['po_id'] = $po_id;
$is_cod =($row['is_cod'])? 'Yes' : ' ';
echo "<tr height='20' data-order_id='".$row['po_id']."'>
<td align='center'><input type='radio' class='case' name='case' value='1'></td>
<td align='center'>".$row['po_id']."</td>
<td align='center'>".$row['customer_name']."</td>
<td align='center'>".$row['order_value']."</td>
<td align='center'>".$row['bill_to_pincode']."</td>
<td align='center'>".$is_cod."</td>
<td align='center'>".$row['shipping_status']."</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
}
?>
问题是它直接执行其他部分而不检查条件。
我需要在不点击过滤器的情况下默认显示数据库中的所有记录。
您的代码没有检查它是否有空值。因此,检查这种方式并执行操作。
if(!empty($_POST[`value1`]) && !empty($_POST[`value2`])) {
//write code here
}
将您的PHP代码编辑到此
$shipping_status = 'readytoship';
$order = 'ASC';
if (!empty($_POST[`value1`]) {
$shipping_status = $_POST[`value1`];
}
if (!empty($_POST[`value2`]) {
$order = $_POST[`value2`];
}
$query = 'select * from purna_orders where shipping_status="'.$shipping_status.'" order by po_created_date "'.$order.'"';
//other stuff
和html到这个
<select name="value1" style="width:51px; height:30px;margin-left:161px; margin-top:-29px; margin-bottom:0px;">
<option value="asc">ASC</option>
<option value="desc">DSC</option>
</select>
您需要检查此
if($_POST[`value1`] != "") && ($_POST[`value2`] != "")) {
//Something your query should be here
}
您的查询在if-else条件之外
$query = "select * from purna_orders where shipping_status='readytoship' order by po_created_date asc ";
删除它应该工作