Jump to content

populating form select menu with sql data


gillms1

Recommended Posts

Hi, if i have the following select menu in a form :

<td>Section</td>
<td>:</td>
<td><select name="section">
<option value="local">Local
<option value="uni">Uni
<option value="business">Business
<option value="sport">Sport
<option value="entertainment">Entertainment
</select></td>

is it possible to have the option values created by what is in my sql database (I have a table called section with those values in it)? If so could you please give me an idea as to how?

Thanks,

Sunny
Link to comment
Share on other sites

[!--quoteo(post=367614:date=Apr 23 2006, 01:00 AM:name=gillms1)--][div class=\'quotetop\']QUOTE(gillms1 @ Apr 23 2006, 01:00 AM) [snapback]367614[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi, if i have the following select menu in a form :

<td>Section</td>
<td>:</td>
<td><select name="section">
<option value="local">Local
<option value="uni">Uni
<option value="business">Business
<option value="sport">Sport
<option value="entertainment">Entertainment
</select></td>

is it possible to have the option values created by what is in my sql database (I have a table called section with those values in it)? If so could you please give me an idea as to how?

Thanks,

Sunny
[/quote]

[code]
<td><select name="section">
<?php
$obj_sections = mysql_query("SELECT * FROM sections");
while ($arr = mysql_fetch_assoc($obj_sections)) {
     echo "<option value=\"".$arr['sectionname']."\">".ucfirst($arr['sectionname'])."</option>";
}
?>
</select></td>
[/code]
Link to comment
Share on other sites

Hi, Thanks for the reply, i changed the code so it looks like below :

<?php
include 'admin_header.php';
$dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}



if (!@mysql_select_db('chronicle')) {
exit('<p>Unable to locate the chronicle ' .
'database at this time.</p>');
}
?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="insertarticle_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Insert Article into MySQL Database </strong></td>
</tr>
<tr>
<td width="71">Editor</td>
<td width="6">:</td>
<td width="350"><input name="editor" type="text" id="editor"></td>
</tr>
<tr>
<td><select name="section">
<?php
$obj_sections = mysql_query("SELECT * FROM sections");
while ($arr = mysql_fetch_assoc($obj_sections)) {
     echo "<option value=\"".$arr['sectionname']."\">".ucfirst($arr['sectionname'])."</option>";
}
?>
</select></td></tr>
<tr>
<td>Headline</td>
<td>:</td>
<td><input name="headline" type="text" id="headline"></td>
</tr>
<tr>
<td>Subheadline</td>
<td>:</td>
<td><input name="subheadline" type="text" id="subheadline"></td>
</tr>
<tr>
<td>Content</td>
<td>:</td>
<td><textarea cols=30 rows=10 name="content" type="text" id="content"></textarea></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</html>


however, i now get a blank screen? have i entered it incorrectly?
Link to comment
Share on other sites

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.