doutuo6048 2016-06-10 20:37
浏览 15
已采纳

Mysqli查询使用$ _get,其中缺少值[重复]

This question already has an answer here:

I have a drop-down box where a user can select a location. Then, there is a text box where they can input a maximum rental price (there will be a few more options but to keep things simple just these in the example). Then this will go to a results.php page and using the $_GET array extract the values and query the database

This works fine if both fields are complete, but if they only wanted to search by location and leave the rent field blank it doesn't work and displays results.php?loc=york&rent= in the URL, which then as I have used the AND function displays no results?

I'm very new to PHP and would very much appreciate anyone who can point me in the right direction or what the correct term to search in google for?

<?php
$location = $_GET['loc'];
$rent=$_GET['rent'];

$result = $mysqli->query("SELECT * FROM dbc_posts WHERE '$location'=city &&'$rent'>rent_price  ORDER BY ID ASC");

?>
</div>
  • 写回答

3条回答 默认 最新

  • drbxr86044 2016-06-10 21:03
    关注

    You can either create 2 queries, or just one with some variables.

    $rent = $_GET['rent'];
    $rent_options = "";
    if(isset($rent)) //add condition
     {
          $rent_options .= "&& \'rent\'>rent_price";
     }
    $mysqli->query("SELECT * FROM dbc_posts WHERE '$location'=city".$rent_options." ORDER BY ID ASC"); 
    

    This way, assuming they chose a rent option, it will be added to the query. If not, it will simply be blank space and be ignored.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?