Jump to content

[SOLVED] Selecting an option from a drop down list, from MySQL database, using PHP


Recommended Posts

I'm really new at the whole PHP thing,

 

i have created a form that allows users to update a record from a database (mysql)

 

at the moment the user has to select the ID they want to update

 

what i am trying to do is create a drop down list with the available ID's for the user to select

 

<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("comreg098hassan", $con);
  $result = mysql_query("SELECT AID FROM Agents");
  while($row_ID = mysql_fetch_array($result))
  {
  echo $row_ID['AID'];
  echo "<br />";
  }mysql_close($con);
?>
<form>
<select name="aid"><?php echo $row_ID['AID']; ?></select>
</form> 

 

 

this code displays all the ID's on the page, but doesnt display in the drop down list,

 

I would really appreciate your help

 

thank-you

The correct syntax for a drop down menu is

<select name="aid">
  <option value="value1">Value 1</option>
  <option value="value2">Value 2</option>
  <option value="value3">Value 3</option>
</select>

 

In PHP it'll be

// start of form/menu
echo '<form>
<select name="aid">';

// produce the <option>'s
while($row_ID = mysql_fetch_array($result))
{
  echo '<option value="'.$row_ID['AID'].'">'.$row_ID['AID'].'</option>';
}

// end the menu/form
echo '</select>
</form>';

 

 

 

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.