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>';

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.