2017-04-07 70 views
0

我想通过HTML表单使用PHP显示我的数据库结果。一旦用户点击提交按钮,根据他们在每个字段中输入的内容,结果应该相应地显示。从HTML表单的数据库显示数据

E.g.如果我在我的下拉菜单中点击“房间”,并且在价格和其他字段的文本字段中指定了特定价格,则应显示与我搜索的内容相关的任何内容。

我能做到这一点的下拉菜单,但我不知道我怎么可以输入多个值,当我点击提交的数据相应地产生。

这是我到目前为止已经完成:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
    <form action="dropdown2.php" method="POST"> 
     <select name="testing" type="text"> 
      <option value="Choose">Choose type of stay</option> 
      <option value="Flat">Flat</option> 
      <option value="Room">Room</option> 
      <option value="Apartment">Apartment</option> 
      <option value="Villa">Villa</option> 
     </select> 
     <p></p> 
     <table> 
     <tr> 
      <td width="14%" class="label">Minimum Price MYR</td> 
      <td width="42%"><input type="text" name="textfield4" id="textfield4" class="text" /></td> 
      <td class="label">Maximum Price MYR</td> 
      <td><input type="text" name="textfield2" id="textfield2" class="text" /></td> 
     </tr> 
     <tr> 
      <td class="label">Bed Rooms</td> 
      <td> 
       <label> 
       <input type="text" name="search" id="textfield5" class="text smalltextarea" /> 
       </label> 
      </td> 
      <td class="label">Bathrooms</td> 
      <td><input type="text" name="textfield3" id="textfield3" class="text" /></td> 
     </tr> 
     </table> 
     <p></p> 
     <input type="submit" value="Submit" name="submit"> 
    </form> 
    <?php 
    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error()); 

    mysql_select_db("test") or die(mysql_error()); 
    if(isset($_POST['testing'])){ 
    $query = $_POST['testing']; 

    $min_length = 3; 

    if(strlen($query) >= $min_length){ 
     $query = htmlspecialchars($query);   
     $query = mysql_real_escape_string($query); 

     $raw_results = mysql_query("SELECT * FROM rooms 
      WHERE (`name` LIKE '%".$query."%') OR (`price` LIKE '%".$query."%') OR (`description` LIKE '%".$query."%')") or die(mysql_error()); 
     if(mysql_num_rows($raw_results) > 0){ 

     while($results = mysql_fetch_array($raw_results)){ 
      echo "<p><h3>".$results['name']."</h3>"."RM " .$results['price']."</p>"."<p>".$results['description']."</p>"; 
     } 

    } 
    else{ 
     echo "No results"; 
    } 
     } 
     else{ 
      echo "Minimum length is ".$min_length; 
     } 
    } 
    ?> 
</body> 
</html> 

我新的PHP和MySQL ...任何帮助表示赞赏!

+2

''mysql_ *'函数从PHP v5.5开始已弃用,自v7.0开始已被删除。他们不应该用于新的代码,应该换成[mysqli](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual /en/book.pdo.php)尽可能等效。 –

+1

''没有类型。 –

回答

0

第一个ID说更改名字的东西多一点调和 还你缺少一个名称或说明字段不知道哪一个(因为名称不类似于数据库字段)

取代MySQL的PDO或mysqli的我用PDO在这个例子中 因为如果升级到PHP 7个MySQL的功能被删除(如建议在上述评论)

,而不是MySQL的世外桃源等,你可以使用一个事先准备好的声明

也查询中缺少的内容是其他职位字段,如评论中的建议,例如有多少卫生间,最低价格,最高价格等(这些字段由用户发布但未在查询中使用) 您对所有这些值使用相同的后置字段您的查询

现在基于您的字段我假设例如有人填写minprice = 12和descr = flat,结果都必须有最低价格12和像平的描述,因此您需要更改或者是你在你的查询使用,是基于其场均填写

这里有点上一个例子,如何去了解它:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
    <form action="" method="POST"> 
     <select name="description" type="text"> 
      <option value="Choose">Choose type of stay</option> 
      <option value="Flat">Flat</option> 
      <option value="Room">Room</option> 
      <option value="Apartment">Apartment</option> 
      <option value="Villa">Villa</option> 
     </select> 
     <p></p> 
     <table> 
     <tr> 
      <td width="14%" class="label">Minimum Price MYR</td> 
      <td width="42%"><input type="text" name="minprice" id="textfield4" class="text" /></td> 
      <td class="label">Maximum Price MYR</td> 
      <td><input type="text" name="maxprice" id="textfield2" class="text" /></td> 
     </tr> 
     <tr> 
      <td class="label">Bed Rooms</td> 
      <td> 
       <label> 
       <input type="text" name="bedrooms" id="textfield5" class="text smalltextarea" /> 
       </label> 
      </td> 
      <td class="label">Bathrooms</td> 
      <td><input type="text" name="bathrooms" id="textfield3" class="text" /></td> 
     </tr> 
     </table> 
     <p></p> 
     <input type="submit" value="Submit" name="submit"> 
    </form> 
    <?php 

     $minDescriptionLength = 3; 
     if(isset($_POST['description'])) { 
      $descr = $_POST['description']; 

      if(strlen($descr) >= $minDescriptionLength) { 
       $connectionConfigArr = array('host' => 'localhost', 'dbname' => 'test', 'user' => 'root', 'password' => ''); 
       // here we are making the database connection, 
       // first param = connection string, second param = user, third param = password 
       $dbh = new PDO(
        'mysql:host=' . $connectionConfigArr['host'] . ';dbname=' . $connectionConfigArr['dbname'], 
        $connectionConfigArr['user'], 
        $connectionConfigArr['password'] 
       ); 


       $bindParamArr = array(); 

       //since we check this one in the start i assumed it's required so we can use it in the base for the query 
       $query = 'SELECT * FROM rooms WHERE `description` LIKE :descr '; 

       //here for each extra field that is filled in we basicaly are going to add it to the query string 
       //and we push the value to the bindParamArray which will after we added the query add the values safely to the query 
       if(isset($_POST['minprice']) && $_POST['minprice']) { 
        $query .= 'AND `price` > :minprice '; 
        array_push($bindParamArr, array('field' => ':minprice', 'value' => $_POST['minprice'], 'type' => PDO::PARAM_INT)); 
       } 
       if(isset($_POST['maxprice']) && $_POST['maxprice']) { 
        $query .= 'AND `price` < :maxprice '; 
        array_push($bindParamArr, array('field' => ':maxprice', 'value' => $_POST['maxprice'], 'type' => PDO::PARAM_INT)); 
       } 
       if(isset($_POST['bathrooms']) && $_POST['bathrooms']) { 
        $query .= 'AND `bathrooms` = :bathrooms '; 
        array_push($bindParamArr, array('field' => ':bathrooms', 'value' => $_POST['bathrooms'], 'type' => PDO::PARAM_INT)); 
       } 
       if(isset($_POST['bedrooms']) && $_POST['bedrooms']) { 
        $query .= 'AND `bedrooms` = :bedrooms '; 
        array_push($bindParamArr, array('field' => ':bedrooms', 'value' => $_POST['bedrooms'], 'type' => PDO::PARAM_INT)); 
       } 

       //we still need to push the description to the array 
       array_push($bindParamArr, array('field' => ':descr', 'value' => '%' . $descr . '%', 'type' => PDO::PARAM_STR)); 


       //here we prepare the query 
       $stmt = $dbh->prepare($query); 

       //here we bind the params safely 
       foreach($bindParamArr as $bParam) { 
        $stmt->bindParam($bParam['field'], $bParam['value'], $bParam['type']); 
       } 

       $stmt->execute(); 

       //use this to get the amount of rows the query returned 
       $rowCount = $stmt->rowCount(); 

       if($rowCount > 0) { 
        //we loop through the stmt fetch function which will iterate through the resultset 
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
         echo "<p><h3>".$row['name']."</h3>"."RM " .$row['price']."</p>"."<p>".$row['description']."</p>"; 
        } 
       } 
       else { 
        echo "No results"; 
       } 
      } 
      else { 
       echo "Minimum length is ".$minDescriptionLength; 
      } 
     } 
    ?> 
</body> 
</html>