dshfjsh_5455 2017-03-10 03:21
浏览 106
已采纳

查询在Mysql中搜索记录

I have two select boxes with the search button. In One select box is fruit and a second is car. Now the issue is 1) If anyone select from the first select box and second select box then displays all records (Means it will display all fruit and car records). 2) If anyone select only First select box then display all records(Means it will display only fruit records) or 3) select second select box then display all records(Means it will display all car records).

Below query, I tried it is displaying It is working for the First issue

SELECT * FROM request WHERE fruit='$fruit' and car='$car'

I need a query for 2) and 3) in single query. Would you help me in this?

  • 写回答

1条回答 默认 最新

  • dongxiee0744 2017-03-10 03:30
    关注

    for this you need to develop a little logic I am guessing as POST method

       if(isset($_POST['search']))
    {
    $con =" 1=1 ";
    if(!empty($_POST['fruit']) && !empty($_POST['car'])){
    $con .=" and want_to_learn='".$_POST['fruit']."' and expert_in='".$_POST['car']."'";
    }
    else if(!empty($_POST['fruit'])){
    $con .=" and want_to_learn='".$_POST['fruit']."'";
    }
    else if(!empty($_POST['car'])){
    $con .=" and expert_in='".$_POST['car']."'";
    }
    
    $search_sql="SELECT * FROM request WHERE ".$con ;
    $search_result = $conn->query($search_sql);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题