表单:使用1个带2个名称的输入框

时间:2015-03-04 12:49:55

标签: html forms

我在这里的第一篇文章,所以希望它不是一个愚蠢的问题。我在导航栏中包含一个搜索框,我希望它按产品名称或描述进行搜索,这可能吗?我到目前为止的代码如下,可以通过一个字段而不是两个

进行搜索
<div class="search_box">
  <form method="post" name="search" id="search" action="../admin/search_box.php">
    <input type="submit" name="submit" value="Find" style="float: right" />
    <div style="overflow: hidden; padding-right: .5em;">
      <input type="text" name="description" style="width:100%;" />
    </div>
  </form>
</div>

然后进行数据处理

<?php 
require ('dbconnection.php');  
$title = filter_var($_POST["title"], FILTER_SANITIZE_STRING);
$description = filter_var($_POST["description"], FILTER_SANITIZE_STRING);

if (isset($_POST['submit'])) // waits for user to enter data into the form
{
    $sql_search_lookup = "SELECT * FROM item WHERE item.description LIKE '%$description%'";

    $result=mysqli_query ($link, $sql_search_lookup) or die ('Problem:'.$sql_search_lookup);

    if (mysqli_num_rows($result) > 0) 
    {                 
        while ($row=mysqli_fetch_array($result))            
        {
?>                              
        <div style="float: left; width: 98%; margin: 10px">
        <p>
            <div style="float:left; width: 20%;">
              <a href="/products_detail.php?itemID=<?php print $row["itemID"]?>">
                <img src="../images/products/<?php print $row['img_file_path'];?> " width="150" height="120">
              </a>
            </div>
            <div style="float:left;width: 75%;vertical-align:middle; font-size:14px; padding:1%;">
              <?php echo "Title: ". $row['title']?><br>
              <?php echo "Description: ".$row['description']?>
            </div>
        </p>
        </div>      
    <?php }
    } 
    else 
    {
        echo "We cant find what you're looking for sorry";
    }
}
    ?>

1 个答案:

答案 0 :(得分:1)

您的HTML应该是:

<div class="search_box">
  <form method="post" name="search" id="search" action="../admin/search_box.php">
    <input type="submit" name="submit" value="Find" style="float: right" />
    <div style="overflow: hidden; padding-right: .5em;">
      <input type="text" name="description" style="width:100%;" />
    </div>
  </form>
</div>

你的mysql查询应该是这样的:

$sql_search_lookup = "SELECT * FROM item WHERE item.description LIKE '%"+$description+"%' or item.title LIKE '%"+$description+"%'";