PHP:无法将从数据库中检索到的值显示为下拉列表

时间:2012-03-19 08:58:40

标签: php html

我有一个任务创建 - 查看 - 编辑页面。一旦我创建了任务,用户就想编辑它。他点击了编辑按钮。所以该值根据id填充。除了Dropdown之外,所有值都会被填充。:

这是我的下拉菜单:

<b>Assignee: &nbsp &nbsp &nbsp &nbsp </b><select name = "assignee" value = <?php echo $assignee ?></select>
<b>Priority:</b><select name = "priority" value= "<?php echo $priority; ?>" id="priority"><option>Low</option><option>Medium </option><option>High</option></select>
<b>Status: </b><select name = "status" value= "<?php echo $status; ?>" ><option>Assigned</option><option>Yet to Start </option><option>In Progress</option><option>Completed</option><option>Blocked</option></select>

这是获取值并显示在表中并更新到数据库

的代码
<?PHP
function renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error) {/connecttothedatabaseinclude ('configdb1.php');
    // check if the form has been submitted. If it has, process the form and save it to the database
    if (isset($_POST['submit'])) {
        // confirm that the 'id' value is a valid integer before getting the form data
        if (is_numeric($_POST['id'])) {
            // get form data, making sure it is valid
            $id = $_POST['id'];
            $task = $_POST['task'];
            $comments = $_POST['comments'];
            $assignee = $_POST['assignee'];
            $priority = $_POST['priority'];
            $status = $_POST['status'];
            $dataum1 = $_POST['dataum1'];
            $dataum2 = $_POST['dataum2'];
            // check that firstname/lastname fields are both filled in
            if ($task == '' || $comments == '') {
                // generate error message
                $error = 'ERROR: Please fill in all required fields!';
                //error, display form
                renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, $error);
            } else {
                // save the data to the database
                mysql_query("UPDATE work SET task='$task', comments='$comments', assignee='$assignee', priority='$priority', status='$status', dataum1='$dataum1', dataum2='$dataum2' WHERE id='$id' ") or die(mysql_error());
                // once saved, redirect back to the view page
                header("Location: view.php");
            }
        } else {
            // if the 'id' isn't valid, display an error
            echo 'Error!';
        }
    } else
    // if the form hasn't been submitted, get the data from the db and display the form
    {
        // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
        if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) {
            // query db
            $id = $_GET['id'];
            $result = mysql_query("SELECT * FROM work WHERE id=$id") or die(mysql_error());
            $row = mysql_fetch_array($result);
            // check that the 'id' matches up with a row in the databse
            if ($row) {
                // get data from db
                // get data from db
                $task = $row['assignee'];
                $comments = $row['2'];
                $assignee = $row['assignee'];
                $priority = $row['priority'];
                $status = $row['status'];
                $dataum1 = $row['dataum1'];
                $dataum2 = $row['dataum2'];
                // show form
                renderForm($id, $task, $comments, $assignee, $priority, $status, $dataum1, $dataum2, '');
            } else
            // if no match, display result
            {
                echo "No results!";
            }
        } else
        // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
        {
            echo 'Error!';
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

select元素没有value属性 - 所选的option具有selected属性。

换句话说,你需要这样的东西:

<select name = "priority" id="priority">
    <option <?php if ($priority == 'Low') { echo 'selected="selected"'; } ?>>Low</option>
    <option <?php if ($priority == 'Medium') { echo 'selected="selected"'; } ?>>Medium </option>
    <option <?php if ($priority == 'High') { echo 'selected="selected"'; } ?>>High</option>
</select>