Jump to content


Photo

populating form select menu with sql data


  • Please log in to reply
2 replies to this topic

#1 gillms1

gillms1
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 April 2006 - 05:00 AM

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

#2 mb81

mb81
  • Members
  • PipPipPip
  • Advanced Member
  • 120 posts

Posted 23 April 2006 - 05:36 AM

[!--quoteo(post=367614:date=Apr 23 2006, 01:00 AM:name=gillms1)--][div class=\'quotetop\']QUOTE(gillms1 @ Apr 23 2006, 01:00 AM) View Post[/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]

<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>


#3 gillms1

gillms1
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 April 2006 - 05:50 AM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users