仅显示下拉列表中的选定行选项

时间:2018-03-08 13:19:02

标签: php mysql

下拉框的值是从表头中的数据库获取的。当用户在下拉菜单中选择其中一个选项[例如:Undelivered]时,我只需要显示值为“未送达”的行....

enter image description here

从数据库中获取值:[正常工作]

$sql = "SELECT DISTINCT in_transit FROM do_order";
$result = mysqli_query ($mysqli, $sql);

echo '<select id="my_select" onchange="send_option();">'; 
echo '<option selected>Status</option>';

while ( $row = mysqli_fetch_array($result) )
{
  echo "<option value='" . $row['in_transit'] . "'>" . $row['in_transit'] . "</option>";
}

echo '</select>';

代码下方显示所选选项行[当用户选择未发送时仅显示未发送]:不工作

$in_transit = mysqli_real_escape_string($conn, $_POST['in_transit']);

        $sql1 = "SELECT in_transit FROM do_order WHERE in_transit LIKE '%$in_transit%'";
        $result = mysqli_query($conn, $sql1);

            if (mysqli_num_rows($result) > 0) 
                {
                    while($row = mysqli_fetch_assoc($result)) 
                        {
                            echo "$row['in_transit']";                             
                        }
                }

1 个答案:

答案 0 :(得分:1)

使用:

if(isset($_POST['in_transit'])){

echo $_POST['in_transit'];

 $sql1 = "SELECT in_transit FROM do_order WHERE in_transit LIKE '%$in_transit%'";
        $result = mysqli_query($conn, $sql1);

            if (mysqli_num_rows($result) > 0) 
                {
                    while($row = mysqli_fetch_assoc($result)) 
                        {
                            echo "$row['in_transit']";                             
                        }
                }
}

ECHO $_POST['in_transit']以确保在运行查询之前获得Post Value。如果你得到Post Value,你可以发表评论。