SELECT help with php and mysql

I have various select drop-down boxes which display dob, position, experience, etc

1. The user fills out the form and data is stored in mysql

2. The user edits the form all of my SELECT boxes are set to default.

How can I display the data from the table as default?  And also update the table if the SELECT changes?
UltraFluxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steezyCommented:
To set the default option for an HTML Select box all you have to do is add "selected" to the <option> tag like this:

<select name="something">
<option value="value1">Value1</option>
<option value="value2" selected>Value2</option>
<option value="value3">Value3</option>
</select>
0
dr_dedoCommented:
say you have that select group
<select name="position">
<option value="1">CEO</option>
<option value="2">Manager</option>
</select>

you save the value of $_POST['position'] in the database and say it is 2, to read it and load the form with option 2 selected do this
<?
mysql_connect ($server,$user,$pass);
mysql_select_db($dbname);
$r = mysql_query ("select * from table where id = xx");
$row = mysql_fetch_assoc($r);
?>
<!-- here you start to print the form //-->
<form method="POST" action="update.php">
<select name="position">
<option value="1" <?php echo (($row['position']==1? ' selected ': '')) ?>>CEO</option>
<option value="2" <?php echo (($row['position']==2? ' selected ': '')) ?>>Manager</option>
</select>
</form>
in update page you do almosr exactly what you did with the insert, except you use different query.
update table name set column1=variable1, table2=variable2

a better way to do things, if you have a table with poistions, you can create your options list dynamically as in the attached code.

<?
mysql_connect ($server,$user,$pass);
mysql_select_db($dbname);
$r = mysql_query ("select * from table where id = xx");
$row = mysql_fetch_assoc($r);
$rPosition = mysql_query ("select id,position from tablePositions");
 
?>
<!-- here you start to print the form //-->
<form method="POST" action="update.php">
<select name="position">
<?php
while ($rowP=mysql_fetch_assoc($rPosition)){
?>
<option value="<?php echo $rowP['id']?>" <?php echo (($row['id']==$rowP['id']? ' selected ': '')) ?>><?php echo $rowP['id']?></option>
<?php
}
?>
</select>
</form>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
UltraFluxAuthor Commented:
Yes I already use a dynamic list ^^

<option value="1" <?php echo (($row['position']==1? ' selected ': '')) ?>>CEO</option>
<option value="2" <?php echo (($row['position']==2? ' selected ': '')) ?>>Manager</option>

This is what does it right?
Whichever item is in the DB will show selected and the rest wont.

dr_dedo I'm posting another question similar to this I just dont want to ask another question in here.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.