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?
 
dr_dedoConnect With a Mentor Commented:
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
 
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
 
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
All Courses

From novice to tech pro — start learning today.