在数据库的下拉列表中显示选定的值

时间:2015-05-20 06:18:39

标签: php database

这是我在编辑表单时尝试显示详细信息但是出错的方法。

<select id="progcode" name="progcode">
    <option value="0">-none-</option>
    <option  value="1" <?php if($progcode == '1') echo 'selected="selected"' ?>>PreS1-AB</option>
    <option value="2" <?php if($progcode == '2') echo 'selected="selected"' ?> >PreS1-MM</option>
    <option value="3" <?php if($progcode == '3') echo 'selected="selected"' ?> >TutEng</option>
</select>

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

正确的格式是打击---这是工作正常,,,,,, 谢谢大家的建议。

<select id="progcode" name="progcode">
                        <option value="0">-none-</option>
                        <option  value="1" <?php if($row["progcode"]==1) echo 'selected'; ?>>PreS1-AB</option>
                        <option value="2" <?php if($row["progcode"]==2) echo 'selected'; ?> >PreS1-MM</option>
                        <option value="3" <?php if($row["progcode"]==3) echo 'selected'; ?>>TutEng</option>
                    </select>

答案 1 :(得分:0)

//here the simple way
<?php
$servername = "localhost";
$user = "root";
$password = "";
$db = "ajaxcall";

$con = mysqli_connect($servername, $user, $password, $db);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<select name="name">
<option value=""> -----------ALL----------- </option> 
<?php
$query = "select DISTINCT id from crud";
$result = mysqli_query($con, $query);
while ($rows = mysqli_fetch_array($result)) {
echo "<option value='$rows[0]'> $rows[0] </option>";
}
?>
</select>
</body>
</html>

&#13;
&#13;
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php
$servername = "localhost";
$user = "root";
$password = "";
$db = "ajaxcall";

$con = mysqli_connect($servername, $user, $password, $db);
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <select name="name">
            <option value=""> -----------ALL----------- </option> 
            <?php
            $query = "select DISTINCT id from crud";
            $result = mysqli_query($con, $query);
            while ($rows = mysqli_fetch_array($result)) {
                echo "<option value='$rows[0]'> $rows[0] </option>";
            }
            ?>
        </select>
    </body>
</html>
&#13;
&#13;
&#13;