How to populate html combo box with mysql data

Please help, I am having problems loading mysql data into combo box. The data I am loading is 1 column from the table. Here is my current code and it crashed firefox for some reason:

<td colspan="2″>Religion</TD>
<td>
<select name="REL" onClick="submitCboSemester();">
<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
while($query_data = mysql_fetch_array($result_disp))
{
?>
<option value="<? echo $query_data["RID"]; ?>"<?php if ($query_data["RID"]==$_POST['REL']) {?>selected<? } ?>><? echo $query_data["RELIGION"]; ?></option>
<? } ?>
</select>
</td>

      

Column RELIGION

, and ID RID

How to fill a field with all data in the RELIGION column

+2


a source to share


5 answers


<html>
<title>
demo </title>
<body>
<?
// Load field datas into List box
$cn=mysql_connect("localhost",root) or die("Note: " . mysql_error());
echo "Conn ok<br>";
$res=mysql_select_db("test",$cn) or die("Note: " . mysql_error());
echo " Database opened<br>";
$q="insert into usha2 (`sno`, `city name`) values (NULL, 'Nagercoil');";
//$q="INSERT into `test`.`usha2` (`sno`, `city name`) VALUES (NULL, 'Delhi');";
$res=mysql_query($q) or die("Note: " . mysql_error());
echo " record inserted<br>";
$res=mysql_query("select * from usha2;") or die("Note: " . mysql_error());
echo " qry executed<br>";
?>
<select name="pno" size=1>
<?
while($ri = mysql_fetch_array($res))
{
echo "<option value=" .$ri['city name'] . ">" . $ri['city name'] . "</option>";
}
echo "</select> "
?>
</body>

      



+3


a source


Chances are you don't have the short_open_tag

included in php.ini

, this is probably causing your code to crash.

Also, you must output selected="selected"

, not onlyselected



Try the following:

<td colspan="2″>Religion</TD>
<td>
<select name="REL" onClick="submitCboSemester();">
<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
while($query_data = mysql_fetch_array($result_disp))
{
?>
<option value="<?php echo $query_data["RID"]; ?>"<?php if ($query_data["RID"]==$_POST['REL']) {?> selected="selected"<? } ?>><?php echo $query_data["RELIGION"]; ?></option>
<? } ?>
</select>
</td>

      

+4


a source


Extract the data before displaying it!

Also, when mixing PHP and HTML, use the Alternative PHP Syntax :

<?php
$query_disp="SELECT * FROM Religion ORDER BY RID";
$result_disp = mysql_query($query_disp, $conn);
$options = array();
while ($query_data = mysql_fetch_array($result_disp)) {
    $options[$query_data["RID"]] = $query_data["RELIGION"];
}
?>

<select name="REL" onClick="submitCboSemester();">
<?php foreach ($options as $key => $value) : ?>
    <?php $selected = ($key == $_POST['REL']) ? 'selected="selected"' : ''; ?>
    <option value="<?php echo $key ?>" <?php echo $selected ?>>
    <?php echo $value ?>
    </option>
<?php endforeach; ?>
</select>

      

+1


a source


It looks like selected

there is no space between the end of your value and there is no space.

0


a source


When you fetch data from your database you should use the following format as the format you are currently using is phased out as MySqli progresses

    $religion = $db->query("SELECT Religion FROM TableName ORDER BY RID");

      

$ db is the connection to the database created as such

    $db = new Mysqli($server, $username, $password, $database);

      

Just set the variable to the correct value.

0


a source







All Articles