Mysqli SELECT加入多个WHERES

时间:2018-01-24 23:42:44

标签: php mysql mysqli

我在咨询数据库mysqli时遇到多个where的问题。我不知道如何制作它。我正在阅读有关JOINS的多个咨询,但我不太了解它,但它仍然无法正常工作。这是我的代码:

$status = mysqli_escape_string($con, $_POST['status']);
$type = mysqli_escape_string($con, $_POST['type']);
$beds = mysqli_escape_string($con, $_POST['beds']);
$baths = mysqli_escape_string($con, $_POST['baths']);
$query = "SELECT * FROM properties WHERE beds LIKE '%".$beds."%' OR baths LIKE '%".$baths."%' " ;

我需要获得所有包含床位,浴室,状态,类型等的结果,但它不适用于实际的SELECT。我已经尝试了JOINS,但这并没有按照我的预期进行。此函数提供所有结果但未过滤。谢谢你的帮助!

编辑:

这是我的所有代码,我的表格

<form action="" id="amenities-form" name="amenities-form" data-name="Amenities Form" class="top-search-form-left" method="POST">
      <select id="status" name="status" data-name="status" class="select-search-form w-select">
        <option value="sale">For Sale</option>
        <option value="rent">For Rent</option>
      </select>
      <select id="type" name="type" data-name="type" class="select-search-form w-select">
        <option value="">Type of property</option>
        <option value="condo">Condo</option>
        <option value="house">House</option>
        <option value="commercial">Commercial</option>
        <option value="lot">Lot</option>
        <option value="villa">Villa</option>
        <option value="business">Business</option>
        <option value="fractional">Fractional</option>
        <option value="boat">Boat</option>
      </select>
      <input type="text" class="input-search-form w-input" maxlength="256" name="beds" data-name="beds" placeholder="Beds" id="beds">
      <input type="text" class="input-search-form w-input" maxlength="256" name="baths" data-name="baths" placeholder="Baths" id="baths">
      <input type="submit" value="Go" class="btn-search-left w-button">
  </form>
  <form action="" id="search-form" name="search-form" data-name="Search Form" class="top-search-form-right" method="POST">
    <input type="text" class="input-search-form w-input" maxlength="256" name="search" data-name="search" placeholder="Search..." id="search-word">
    <input type="submit" value="Lupa" class="btn-search-right w-button">
  </form>

这是我的php代码

if (!empty($_POST)) {
$con = new mysqli($host, $user, $pass, $db);

if (mysqli_connect_errno()) {
  printf("Falló la conexión failed: %s\n", $con->connect_error);
  exit();
}
if (isset($_POST['search'])) {
  $search = mysqli_escape_string($con, $_POST['search']);
  $query = "SELECT * FROM properties WHERE title LIKE '%".$search."%'" ;
}else{
  $status = mysqli_escape_string($con, $_POST['status']);
  $type = mysqli_escape_string($con, $_POST['type']);
  $beds = mysqli_escape_string($con, $_POST['beds']);
  $baths = mysqli_escape_string($con, $_POST['baths']);
  $query = "SELECT * FROM properties WHERE beds LIKE '%".$beds."%' OR baths LIKE '%".$baths."%' " ;
}

$res = $con->query($query);

if (!$res) {
  trigger_error('Invalid query: ' . $con->error);
}

if ($res->num_rows) {
  while ($row = $res->fetch_object()) {
    //$status_name = $con->query("SELECT * FROM status WHERE id = {$row->status_id}");
    //$status_name = $status_name->fetch_object();
    //echo "{$row->title} ({$status_name->title})<br>";
    echo "{$row->title} ({$row->status_id})<br>";
  }
}else{
  echo "No results";
}

$res->free();

$con->close();
}else{
  echo 'No results';
}

我会尝试一些建议你做了

0 个答案:

没有答案