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/>";
    }

?>

Dani AI

Generated

you are very close. The main issues are: (1) you are trying to use the loop variable for city after the loop has finished, so it is empty; (2) your <select> should open once before the loop, add <option> rows inside, then close after; (3) read the selected value from the field name you gave the <select> (e.g. dcity) using $_POST, not from an unrelated $_REQUEST['city']; and (4) quote array keys like $row['doctor'] and $row['hospital'], and always put quotes around HTML attribute values.

Here is a compact, safe pattern that keeps everything on the same page and uses prepared statements:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8mb4','user','pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$cities   = $pdo->query('SELECT DISTINCT city FROM docinfo ORDER BY city')
               ->fetchAll(PDO::FETCH_COLUMN);
$selected = $_POST['dcity'] ?? '';
?>
<form method="post">
  <select name="dcity">
    <option value="">Choose a city...</option>
    <?php foreach ($cities as $c): ?>
      <option value="<?php echo htmlspecialchars($c, ENT_QUOTES); ?>"
              <?php echo $c === $selected ? 'selected' : ''; ?>>
        <?php echo htmlspecialchars($c); ?>
      </option>
    <?php endforeach; ?>
  </select>
  <button type="submit">Go</button>
</form>
<?php
if ($selected !== '') {
    $stmt = $pdo->prepare('SELECT doctor, hospital FROM docinfo WHERE city = ?');
    $stmt->execute([$selected]);
    foreach ($stmt as $row) {
        echo 'Doctor: ' . htmlspecialchars($row['doctor']) .
             ', Hospital: ' . htmlspecialchars($row['hospital']) . '<br>';
    }
}

Notes: keep the <select> outside the while loop (contrary to ’s suggestion) or you will render many selects. correctly spotted why your city was empty: the variable you echoed was out of scope. Also, favor mysqli/PDO over the old mysql_* API and use prepared statements to prevent SQL injection. See MySQL extension deprecation notice and PDO prepared statements. (php.net)

Recommended Answers

All 4 Replies

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.