Help with getting info from my database using radio buttons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

Help with getting info from my database using radio buttons

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post 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' "); }
 
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

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

Post 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???
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post 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()); }
 
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

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

Post by dthomas31uk »

Hi Aceconcepts....get no error it just displays $price = Resource id #6

AARRRGGHH
User avatar
omika
Forum Newbie
Posts: 19
Joined: Sun Oct 12, 2008 2:00 pm
Location: New Zealand

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

Post 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'];
}
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

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

Post 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.
User avatar
omika
Forum Newbie
Posts: 19
Joined: Sun Oct 12, 2008 2:00 pm
Location: New Zealand

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

Post 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;
Post Reply