Please help out as i am a beginner in PHP
I have a table with the name doinfo with the fields doctor,hospital, and city.
What I want is that when I select city from the dropdownlist, it should give me its details i.e the doctor and hospital.

I am able to get the city from the table in the drop down list,but when i select the 1 of the cities i am unable to get their details from the database.
here is the code I have done so far

<?

$query = "SELECT DISTINCT `city` FROM `docinfo`";

$result = mysql_query ($query);

while($nt=mysql_fetch_array($result))
{ 
echo "<option value=$nt[city]>$nt[city]</option>";

}
echo "</select><input type='submit' name='submit' value='Go'><br>";

$q = "SELECT `doctor`, `hospital` FROM `docinfo` WHERE `city` = '$nt[city]'";
$r = mysql_query($q);




while($row = mysql_fetch_array($r))
    {
    echo "Doctor:".$row[doctor].", Hospital name:".$row[hospital]."<br/>";
    }

?>

correct
PLZ MAKE CHANGES IN WHILE LOOP INSERT "<SELECT>"
while($nt=mysql_fetch_array($result))
{
echo "<select><option value=$nt[city]>$nt[city]</option>";
}
echo "</select><input type='submit' name='submit' value='Go'><br>";

try
echo this following line
$q = "SELECT `doctor`, `hospital` FROM `docinfo` WHERE `city` = '$nt[city]'";

May be the $nt[city] value is null in the query
In form action provide
action="actionp.php?city=$nt[city]"
and in action.php receive the passed url values as
$city=$_REQUEST;
and use it in mysql query.
Your action.php page can be any page, may be the same page
Then before retrieving the doctor information put them in a condition as
if(isset($_REQUEST))
Neither it block of code will execute every time the pahe will load.

Thanks mahavir123
that's right when I echo my query, it shows no value at city=''.
I followed the steps you suggested after that, but nothing seems working.
I want to display the information in the same page..

the code looks like this after making changes..

let me know where am i making mistake..

echo "<form name=input method=post action=assigndoc.php?city=$nt[city]>";
echo "<select name=dcity value=''>Asign Doctors</option>";

while($nt=mysql_fetch_array($result))
{

echo "<option value=$nt[city]>$nt[city]</option>";
}
echo "</select><input type='submit' name='submit' value='Go'><br>";



$city = $_REQUEST['city'];

    if(isset($_REQUEST['submit']))
        {
            $q = "SELECT `doctor`, `hospital` FROM `docinfo` WHERE `city` = '$city'";

            $r = mysql_query($q);
            while($row = mysql_fetch_array($r))
                {
                    echo "Doctor:".$row[doctor].", Hospital name:".$row[hospital]."<br/>";
                }
echo "</form>";
            }

assigndoc.php is the page where i have the dropdown, and want to display details of the doctors here itself

ok.. I caught my mistake.Thanks alot for the same

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.