Jump to content

Drop Down User Selected Mysql


Recommended Posts

I'm new to PHP so please go easy. I apologize if this question has already been answered; I've looked and could not find it. If so, could someone please point me in the right direction.I have 2 tables within my mysql database... One table (users) contains user registration information and the other table (options) is used for populating a drop down menu (ex: state, country, etc). I've successfully created the drop down menu which is populated from the 'options' table in my database. This is all used for user registration and when submitted it inserts the form data into the 'users' table in my database. Works great.The problem is that I need for users to be able to update their personal information. I need the drop down menu from the form to be populated from the 'options' table (which works), but also need the users state selection from the 'users' table to be the selected option. Basically they should be able to see which state they registered previously. I hope this makes sense.Here's the code:

  <form action='personal-info.php' method="POST">  // Rest of Form  <SELECT name="states"> <?php $query="SELECT states FROM options"; $result=mysql_query($query);  while ($row=mysql_fetch_array($result)) { $list=$row["states"]; echo "<OPTION>".$list."</OPTION>"; } ?> </SELECT>  <input type="submit" name="submit" value="Update"/> </form>

I've been fighting with this for a couple of days. Any help would be greatly appreciated. Thanks.

Link to comment
Share on other sites

Firstly, you are missing a VALUE from the <option> value

<SELECT name="states"><?php$query="SELECT states FROM options";$result=mysql_query($query);while ($row=mysql_fetch_array($result)){	echo "<option value=\"".$row["states"]."\">".$row["states"]."</option>";}?></select>

now if you are trying to set a default value of state? something simlar to this

<?php$uquery=mysql_query("SELECT * FROM user WHERE id='$USERID?'"); //VALUES WILL NEED TO BE CHANGED this is just an example of how to get the user data. (this assums your table is called user and your using a primary key ID and $USERID is something your using to identify each user (such as a session))$surecord=mysql_fetch_array($uquery) //this is to later use where the checking of the user state is $query="SELECT states FROM options";$result=mysql_query($query);while ($row=mysql_fetch_array($result)){  if($surecord['state']==$row["states"]) //This row checks to see if the users state is currently equal to the state it's looping through and if it is it select this as the default value. (This assums your user state is stored in a field called state)  {   echo "<option selected=\"selected\" value=\"".$row['states']."\">".$row['states']."</option>";  }  else  {	echo "<option value=\"".$row['states']."\">".$row['states']."</option>";  }}?>

Anyone is welcome to correct me as i've probably made a few mistakes this being a quick mock up.

Edited by morrisjohnny
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...