php - display data from database of an HTML form -
i trying display results database using php through html form. once user clicks submit button, based on enter in each field, results should shown accordingly.
e.g. if click on "rooms" in drop down menu, , when specify price in text field price , rest of fields, related searched should displayed.
i able drop down menu, don't know how can enter multiple values , when click submit generates data accordingly.
here's i've done far:
<!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 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 * rooms (`name` '%".$query."%') or (`price` '%".$query."%') or (`description` '%".$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 ".$min_length; } } ?> </body> </html>
i'm new php , mysql... appreciated!
first id change names bit more reconcilable missing name or description field not sure 1 (since names don't resemble database fields)
replace mysql pdo or mysqli used pdo in example since if upgrade php 7 mysql functions removed (like suggested in comments above)
instead of mysql real escape etc can use prepared statement
also missing in query other post fields suggested in comments example how many bathrooms, minprice, maxprice etc (these fields posted user not used in query) using same postfield of values in query
now based on fields assuming example if fills in minprice = 12 , descr = flat result both has have minimum price of 12 , description flat need change or's used in query and's based on field filled in
here bit of example on how go it:
<!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 making 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 check 1 in start assumed it's required can use in base query $query = 'select * rooms `description` :descr '; //here each field filled in basicaly going add query string //and push value bindparamarray after added query add values safely 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 push description array array_push($bindparamarr, array('field' => ':descr', 'value' => '%' . $descr . '%', 'type' => pdo::param_str)); //here prepare query $stmt = $dbh->prepare($query); //here bind params safely foreach($bindparamarr $bparam) { $stmt->bindparam($bparam['field'], $bparam['value'], $bparam['type']); } $stmt->execute(); //use amount of rows query returned $rowcount = $stmt->rowcount(); if($rowcount > 0) { //we loop through stmt fetch function iterate through 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 ".$mindescriptionlength; } } ?> </body> </html>
Comments
Post a Comment