472,141 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

PHP edit form for MySQL database using <Select> fields

Hi,

I have a php form that encodes the responses to various <Select> field
drop down menus as numbers (e.g. 0 to 50), and then posts the data to
a MySQL table. I am trying to implement a form to allow editing of the
data already in the table. I can read the data out of the database
without trouble, however I am having trouble figuring out a way to have
the appropriate option in the <Select> fields chosen to reflect the
data already in the database. Does anyone have any suggestions on how I
can accomplish this?

Best wishes,

George Hadley
gh********@yahoo.com

Jun 18 '06 #1
1 8438
gh********@yahoo.com wrote:
[snip]
I am having trouble figuring out a way to have
the appropriate option in the <Select> fields chosen to reflect the
data already in the database. Does anyone have any suggestions on how I
can accomplish this?


Sure.

$data = mysql_fetch_array(mysql_query('SELECT fieldname FROM table WHERE
id='.$theId));

echo '
<select name="mylist">
<option value="option1"'; if ($data['fieldname'] == 'option1') { echo '
selected="selected"'; } echo '>Option 1</option>
<option value="option2"'; if ($data['fieldname'] == 'option2') { echo '
selected="selected"'; } echo '>Option 2</option>
';
....etc...
echo '</select>';

This is how I do these things atleast. The point is to check if the
option is the one in the db, and if it is, add selected="selected" (or
just selected if you're not using xhtml).
Roy W. Andersen
--
ra at broadpark dot no / http://roy.skyggenesdal.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jun 19 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Astra | last post: by
5 posts views Thread by Brian Foley | last post: by
6 posts views Thread by Chris Fink | last post: by
5 posts views Thread by Sonnich | last post: by
7 posts views Thread by Shrek | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.