I’m having trouble loading the mysql data to the option box using php. Here’s my code:
<?php
$con = mysql_connect("localhost","myuname","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("school", $con);
$idnum= mysql_real_escape_string($_POST['idnum']);
$result = mysql_query("SELECT * FROM student WHERE IDNO='$idnum'");
?>
<?php while ( $row = mysql_fetch_array($result) ) { ?>
<tr>
<td width="30" height="35"><font size="2">*I D Number:</td>
<td width="30"><input name="idnum" type="text" maxlength="5" value="<?php echo $row["IDNO"]; ?>" readonly="readonly"></td>
</tr>
My problem is loading it here:
<td><font size="2">Gender</td>
<td>
<select name="gender" id="gender">
<font size="2">
<option value="<?php echo $line['IDNO']; ?> "><?php $line['GENDER'] ; ?></option>
</select></td></td>
The table looks like this:
IDNO | GENDER
123 | M
321 | F
What am I supposed to do?To load the exact gender corresponding to the IDNO?
Note: You are wrongly using the
$lineinstead of$rowfor the options, hence not getting the result.Note 2: From your query it looks, you are fetching just one record:
And hence there is no need for the loop (I assume
IDNOis primary key of the table). If however, you want to read all records of the table, your query should be something like this:How To Go About:
Put the select box before the
whileloop:Then your
whileloop for options:Close the
whileloopAnd finally close the select box
So in all, your code should look something like this (excluding db stuff):