PHP-使用选择选项在sql表上搜索

时间:2018-06-27 12:28:03

标签: php html database search mysqli

当我在输入中键入并选择选项以显示数据库中的信息时,我想这样做,但是我没有这样做。这是我在项目上要做的最后一件事,但对我来说很难。我是PHP新手。感谢您对我的帮助!非常感谢。

我这里有HTML代码:

<div name="search-select">
<form method="post" action="">
<input type="text" name="info" value="Search input">
<select name="select_room">
<option>Select a room</option>
  <option value="1 room">1 room</option>
  <option value="2 rooms">2 roomns</option>
  <option value="3 rooms">3 rooms</option>
  <option value="4 or more">4 or more</option>
</select>


<select name="select_floor">
<option>Select the floor</option>
  <option value="1">First floor</option>
  <option value="2">Second floor</option>
  <option value="3">Third floor</option>
  <option value="4 or more">Fourth floor or more</option>
</select>


<select name="select_location">
<option>Select the location</option>
  <option value="New York">New York</option>
  <option value="Florida">Florida</option>
  <option value="Los Angeles">Lost Angeles</option>
  <option value="Las Vegas">Las Vegas</option>
</select>
<input type="submit" name="rent-search" value="Search">
</form>
</div>

这是我尝试编写的PHP代码,但给我错误。这是MySQLi的原因,因为我尝试在托管服务器上创建它。

$conn = mysqli_connect('localhost', 'rent', 'renthouse');

if (!$conn) {
    die('Could not connect: ' . mysqli_error());
}

$select_room=$_POST['select_room'];
$select_floor=$_POST['select_floor'];
$select_location=$_POST['select_location'];

$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM rent" . (!empty($sql) ? " WHERE " . $sql: '');
$sql = array();

if (!empty($select_room)) {
    $sql[] = "select_room='$select_room'";
}

if (!empty($select_floor)) {
    $sql[] = "select_floor='$select_floor'";
}

if (!empty($select_location)) {
    $sql[] = "select_location='$select_location'";
}


$result = mysqli_query($sql);

if (mysqli_num_rows($result) === 0) {
    echo 'Result not found';
}

while ($row = mysqli_fetch_array($result)) {
   echo $row['select_room'] . '<br/>';
   echo $row['select_floor'] . '<br/>';
   echo $row['select_location'];
}
?>

0 个答案:

没有答案