duanmangxie7131 2015-04-04 12:49
浏览 48

用户输入多个表单域以通过php从mysql表中检索相关记录

<html>
    <form action="search_book.php" method="post">
        Title:<input type="text" name="title"><br>
        Author:<input type="text" name="author"><br>
        Price of Book:<input type="text" name="price"><input type="radio"    name="radio" id="less"><Label for="less">Less Than</label><input type="radio" name="radio" id="more"><label for ="more">More Than</label<<br>
        <input type="submit" value="Search">
    </form>
</html>

I have used an if/else if statement and post function when user searches either by title, author or price(html form fields) for records in a mysql table. My if/else if statement that is based on user inputting one form field at a time:

if($_POST['title']!="" && $_POST['title']!=" "){
    $title=$_POST['title'];
    $query=mysqli_query($con,"SELECT * from book where Title='$title'");
} else if($_POST['author']!=""&& $_POST['author']!=" "){
    $title=$_POST['author'];
    $query=mysqli_query($con,"SELECT * from book where Author='$author'");
} else if($_POST['price']!="" && $_POST['price']!=" "){
    $title=$_POST['price'];
    $query=mysqli_query($con,"SELECT * from book where Price='$price'");
}

I need to take it to the next level now and want to know:

  1. How do I retrieve table records if the user inputs two or three fields and not just one(eg. author and price or author and title)

  2. Near the 'price' text field I need to add two radio buttons which will allow the user to check less than or more than the price he inputs in the textbox for price

  • 写回答

1条回答 默认 最新

  • dsdv76767671 2015-04-04 13:26
    关注
       // Use the Below code it will work for all
       $condition=""; 
       if(isset($_POST) && !empty($_POST)) { 
         if(trim($_POST['title'])!="") {
            $condition.="Title='".$_POST['title']."' AND "; 
         }
        if(trim($_POST['author'])!="") {
          $condition.="Author='".$_POST['author']."' AND "; 
        }
        if(trim($_POST['price'])!="") {
           $condition.="Price='".$_POST['price']."' AND "; 
        }
          // remove last AND 
          if($condition!="") {
              $condition=" WHERE ".substr($condition,0,-5); 
          }
    
         $query=mysqli_query($con,"SELECT * from book".$condition); 
      } 
    

    // It will work

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line