PHP multiple range SELECT

Hello guys!

I have a script that filters database data within a range using a select menu.
However I want to add more options to select like “price range” and filter by a specific row data.

Here is what I have now:


<form action="keres2.php" method="post">
	<select name="range" class="black" id="range">
	<option value="0-500">0-500</option>
	<option value="501-1000">501-1000</option>
	<option value="1001-2000">1001-2000</option>
	<option value="2001-3000">2001-3000</option>
	<option value="3001-999999">3000 +</option>
  <option value="0-999999">All</option>
	</select>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>



<?php
if(isset($_POST['submit'])) {
	$dbHost = "localhost";
	$dbUser = "root";
	$dbPass = "";
	$dbName = "teszt89";
	$db = mysql_connect($dbHost,$dbUser,$dbPass);
	mysql_select_db($dbName,$db);

	$range = $_POST['range'];
	$range = explode('-',$range);
	$query = mysql_query("SELECT * FROM estate_detail WHERE area BETWEEN $range[0] AND $range[1]") or die(mysql_error());
	$numrows = mysql_num_rows($query);
	if($numrows <= 0) {
		echo 'No result.';
	} else {
		while($row = mysql_fetch_array($query)) {
			echo '
      '.$row['title'].'<br>
      '.$row['city'].'<br>
      '.$row['address'].'<br>
      ';
		}
	}
}
?>

What I would like to achive is:

<form action="keres2.php" method="post">

  Select city:  (sql table row: "estate_cat". this row contains plain text -word- like: szeged) 
  <select>
  <option value="szeged">Szeged</option>
  <option value="pecs">Pécs</option>
  <option value="debrecen">Debrecen</option>
  <option value="miskolc">Miskolc</option>
  </select>

  Select price range   (sql table row: "price_1")
  <select>
  <option value="50000-100000">50.000-100.000 HUF</option>
  <option value="100001-200000">100.001-200.000 HUF</option>
  <option value="200001-300000">200.001-300.000 HUF</option>
  <option value="300001-500000">300.001-500.000 HUF</option>
  <option value="0-999999">No price limit</option>
  </select>

  Select area (m2)
	<select name="range" class="black" id="range">
	<option value="0-500">0-500</option>
	<option value="501-1000">501-1000</option>
	<option value="1001-2000">1001-2000</option>
	<option value="2001-3000">2001-3000</option>
	<option value="3001-999999">3000 +t</option>
  <option value="0-999999">All sizes</option>
	</select>
  
<input type="submit" name="submit" value="Submit" />

Is it possible to combine this?

if you use proper naming for your inputs, you can add as many filters as you want. but be aware of SQL-Injections if you don’t want anybody to delete your whole database. use prepared statements.

Actually I’m kinda new to PHP. Can you shom me an example?

Well the first thing I will mention is that the mysql API is old to php, it is no longer in the current versions and deprecated in still supported older versions. No one should still be using it. Certainly a newcomer should not be learning obsolete code.
I would recommend using PDO, but you could use mysqli if you prefer.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.