Jump to content

Min/max price range query


bionic25

Recommended Posts

I'm having difficulties creating a self-containing .php page which has drop downs for price ranges eg £0 < £1000, £1000 < £2000 etc.

I then want it to query a mysql database called "cars" and field "price" and then display the results.

 

code so far:

 

<form action=\"<?php $_SERVER['PHP_SELF'] ?>\" method=\"post\">
Lowest price:<input type=\"text\" name=\"low\">
Highest price:<input type=\"text" name=\"high\">
<input type=\"submit\" value=\"submit\" action=\"submit\">
</form>

<?php

if (isset($_POST['Submit']))
{ 
mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$low	=	$_POST['low'];
$high	=	$_POST['high'];

$result = 		mysql_query("select * from cars WHERE price BETWEEN $low and $high");

while($r=mysql_fetch_array($result))
	{
		$carName=$r["carName"];
		$description=$r["description"];
		echo
		"
		$carName $description
		";
	}
}
else
{
echo"
<form action=\"<?php $_SERVER['PHP_SELF'] ?>\" method=\"post\">
<input type=\"text\" name=\"low\">Lowest price:
<input type=\"text" name=\"high\">Highest price:
<input type=\"submit\" value=\"submit\" action=\"submit\">
</form>
";	
?>

 

This just doesn't work at all. Anyone know where I'm going wrong?

 

(this is one of the first scripts I've written from scratch)

 

Thanks,

 

~bionic25

 

Link to comment
https://forums.phpfreaks.com/topic/119864-minmax-price-range-query/
Share on other sites

i BELIEVE that variables are case-sensitive.  try changing $_POST['Submit'] to $_POST['submit'].  i would also be wary of letting the users input the price range - as you said, you'd be better off using a select dropdown list.  i'm also not sure why you echo the form twice (once at the start and once in the else{}).

it'll be in the $_SERVER['PHP_SELF'] - you're missing both an echo and a semicolon to end the line.  when nothing shows on a page, it's usually because of a parse error that's not being output.  plug this into the top of your file (before editing the $_SERVER part):

 

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>

 

and you'll suddenly see some notifications of where the script is going astray.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.