使用选择选项作为搜索动态字段

时间:2016-05-05 03:22:20

标签: php html sql

我正在建立一个与数据库链接的网站。它根据预算原因对交易进行排序。对于商店搜索部分,我有一个select下拉菜单。我已将数据链接起来,以便下拉列表仅显示数据库中的存储,并在动态更改时自动添加它们。

我的问题是我需要一种方法来实际使用select选项作为搜索字词。这是初始页面。

<!DOCTYPE html>
<html>
<head>
<title>Output 1</title>
</head>
<body>
<h1>Required Output 1</h1>
<h2>Transaction Search</h2>
<form action="output1out.php" method="get">
    Search Store:<br/>
    <input type="text" name="StoreName">
    <br/>
    <input name="Add Merchant" type="submit" value="Search">
</form>
<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);

if(mysqli_connect_error()){
    die("Database Connection Failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")");
};

$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
    die("Database Query Failed!");
};

$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";
$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
    die("Database Query Failed!");
};

echo '<select name="search_string" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){
    echo '<option value="'.$row["StoreID"].'">';
    echo $row["StoreName"];
    echo '</option>'."\n";
};
echo '</select>'."\n";

mysqli_free_result($result);
mysqli_close($connection);
?>

这是输出页面。

<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';

$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);

$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>

Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
    <tr>
        <th width="15%">Item Name</th>
        <th width="15%">Item Price</th> 
        <th width="15%">Purchase Time</th>
        <th width="15%">Purchase Date</th>
        <th width="15%">Category</th>
        <th width="15%">Rating</th>
    </tr>
</table>
<?php
if($result->num_rows > 0){
    // output data of each row
    while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
    <tr>
        <td width="15%"><?php echo $rows['ItemName']; ?></td>
        <td width="15%"><?php echo $rows['ItemPrice']; ?></td>
        <td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
        <td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
        <td width="15%"><?php echo $rows['Category']; ?></td>
        <td width="15%"><?php echo $rows['Rating']; ?></td>
    </tr>
<?php
    }
}
?>

我可以定期进行打字搜索,但我无法想到使用相同方法进行搜索的方法。有可能吗?

0 个答案:

没有答案