开发者

How to populate html combo box with mysql data

开发者 https://www.devze.com 2022-12-29 18:44 出处:网络
Please help, I\'m having trouble loading mysql data on the combo box. The data that I\'m loading is 1 column from a table.

Please help, I'm having trouble loading mysql data on the combo box. The data that I'm loading is 1 column from a 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>

The colu开发者_高级运维mn is RELIGION and it ID is RID How do I populate the combo box with all the data in the column RELIGION


Chances are, you don't have short_open_tag enabled in php.ini, this is probably what was causing your code to fail.

Also, you should output selected="selected", not just selected

Try this:

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


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


Extract the data before displaying it !

And also, use the PHP Alternative Syntax when mixing PHP & HTML :

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


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


When you pull the data from your database you should use the following format as the format you are currently using is being phase out as MySqli advances

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

$db is a connection to the database created as such

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

Just set the variable to the correct value.

0

精彩评论

暂无评论...
验证码 换一张
取 消