Page 1 of 1

Help with getting info from my database using radio buttons

Posted: Mon Oct 13, 2008 8:23 am
by dthomas31uk
Hi Have two drop down lists one that allows a user to select a country with the variable called $cat
The other drop down list allows a user to select a city from the country selected the variable for the city selected is $subcat.

What I have done next is display two radio buttons one that asks the user if it is a full load and one for half load. I have manged to get the value selcted from the radio button and display it, but cannot get the price displayed from my database for full load or half load.

the following is the code I have used but it just wont display the price

Code: Select all

if ($load =='full_load'){ 
$price=mysql_query("SELECT DISTINCT full_price from eu_place where city=$subcat"); }
else {$price=mysql_query("SELECT DISTINCT half_price from eu_place where city=$subcat"); }
 
echo "Value of \$price = $price";

The following is all the code for the page

Code: Select all

<?php
include 'dd.php';
?>
 
 
 
<!doctype html public "-//w3c//dtd html 3.2//en">
 
<html>
 
<head>
<title>Demo Multiple drop down list box from plus2net</title>
</head>
 
<body>
<?php
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
$load=$_POST['load'];
echo "Value of \$cat = $cat <br>Value of \$subcat = $subcat"."<BR>";
echo "Value of \$load = $load";
 
 
if ($load =='full_load'){ 
$price=mysql_query("SELECT DISTINCT full_price from eu_place where city=$subcat"); }
else {$price=mysql_query("SELECT DISTINCT half_price from eu_place where city=$subcat"); }
 
echo "Value of \$price = $price";
?>
 
</body>
 
</html>
 

Hope someone can advise

I know I can get the value as I do the following at it displays correctly

Code: Select all

if ($load =='full_load')
{ 
$price='hi'; }
else {$price= 'bye'; }
 
echo "Value of \$price = $price";
but when I try the mysql query it does not do anything

Re: Help with getting info from my database using radio buttons

Posted: Mon Oct 13, 2008 8:30 am
by aceconcepts
Place some single quotes around you variable:

Code: Select all

 
if ($load =='full_load'){
$price=mysql_query("SELECT DISTINCT full_price from eu_place where city='$subcat' "); }
else {$price=mysql_query("SELECT DISTINCT half_price from eu_place where city='$subcat' "); }
 

Re: Help with getting info from my database using radio buttons

Posted: Mon Oct 13, 2008 8:36 am
by dthomas31uk
Have tried that with quotes around the variabe but it returns the following

$price = Resource id #6

Don't know where that came from the value should be 50

Any ideas???

Re: Help with getting info from my database using radio buttons

Posted: Mon Oct 13, 2008 9:05 am
by aceconcepts
Use mysql_error() to help find the problem:

Code: Select all

 
if ($load =='full_load'){
$price=mysql_query("SELECT DISTINCT full_price from eu_place where city='$subcat' ") or die(mysql_error()); }
else {$price=mysql_query("SELECT DISTINCT half_price from eu_place where city='$subcat' ") or die(mysql_error()); }
 

Re: Help with getting info from my database using radio buttons

Posted: Mon Oct 13, 2008 11:44 am
by dthomas31uk
Hi Aceconcepts....get no error it just displays $price = Resource id #6

AARRRGGHH

Re: Help with getting info from my database using radio buttons

Posted: Mon Oct 13, 2008 9:36 pm
by omika
It can be done better than this but...

Code: Select all

 
while($pri = mysql_fetch_array($price){
echo "Value of \$price = ".$pri['full_price'].$pri['half_price'];
}
 

Re: Help with getting info from my database using radio buttons

Posted: Tue Oct 14, 2008 6:20 am
by aceconcepts
Have you checked that $_POST['subcat'] actually posts a valid value?

Whenever MySQL returns RESOURCE... it means that the query did not execute properly - there is an error somewhere. Probably your $subcat variable.

Re: Help with getting info from my database using radio buttons

Posted: Tue Oct 14, 2008 2:12 pm
by omika
The query did execute, just not as expected. Resource id is the internal location where the mysql_query() result is stored.
I would try

Code: Select all

city='".$subcat."'
or try to find out if it is by doing...

Code: Select all

if ($load =='full_load'){
 $q= "SELECT DISTINCT full_price from eu_place where city='".$subcat."'";
 $price=mysql_query($q) or die(mysql_error()); }
 else {
 $q= "SELECT DISTINCT half_price from eu_place where city='".$subcat."'";
 $price=mysql_query($q) or die(mysql_error()); }
 
 echo $q;