如何在使用PHP从html select标签提交值后返回MySQL表

时间:2014-03-11 22:43:38

标签: php html mysql html-select

我一直试图找到一种方法来做到这一点,但在我的许多搜索中没有运气,所以我希望有人可以帮助我。我想要做的是有一个带有各种值的下拉样式<select>标记以及一个提交按钮。用户可以从列表中选择一个项目,点击提交按钮,并使用某种if语句返回一个表,该表列出了与最初提交的列表项相关的所有项。为了澄清,我基本上有一个包含各种产品的数据库表,我希望select标签用作标准搜索,它将返回数据库中与所选产品/项目相关的所有项目。 例如:

<select>
option-hats
option-shirts
option-pants
</select>
submit button

假设我选择了帽子,然后按下提交按钮,我希望输出是我数据库中所有帽子的表格,其他相关信息如价格,大小,类型等。我显然想要一组不同的数据根据我选择的选项以表格形式显示。

<?php
$con=mysqli_connect("localhost","user","password","gunindex");
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
If(//whatever needs to be here){
    echo //table corresponding to the chosen option;

 ?>

<form action="" method="get">
<select name="calibre[]">
     <option value="500swmagnum">.500 S&W Magnum</option>
     <option value="22lr">.22 LR</option>
    </select><br />
     <input type="submit" value="Submit" />
</form>

我希望响应提交一个看起来像这样的选项:

<?php
$con=mysqli_connect("localhost","user","password","gunindex");
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM ammo WHERE Type='Centerfire'");
        echo"<table border='1'>
            <tr>
            <th>Calibre</th>
            <th>Brand</th>
            <th>Price</th>
            <th>Type</th>
            <th>Name</th>
            <th>Quantity</th>
            <th>Rating</th>
            <th>Retailer</th>
            <th>Gun Type</th>
            <th>Bullet Type</th>
            <th>Grain</th>
            </tr>";
while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['Calibre'] . "</td>";
    echo "<td>" . $row['Brand'] . "</td>";
    echo "<td>" . $row['Price'] . "</td>";
    echo "<td>" . $row['Type'] . "</td>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Quantity'] . "</td>";
    echo "<td>" . $row['Rating'] . "</td>";
    echo "<td>" . $row['Retailer'] . "</td>";
    echo "<td>" . $row['Gun Type'] . "</td>";
    echo "<td>" . $row['Bullet Type'] . "</td>";
    echo "<td>" . $row['Grain'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

mysqli_close($con);
?>

老实说,我不知道该做什么,但我必须从某个地方开始。希望这会有所帮助。谢谢!

0 个答案:

没有答案