2012-03-19 73 views
0

我有一個任務創建 - 查看 - 編輯頁面。一旦我創建任務並且用戶想要編輯它。他點擊編輯按鈕。所以值根據id填充。所有的價值被填充除了下拉:PHP:無法顯示從數據庫檢索到的值到下拉

這是我的下拉菜單:

<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!'; 
     } 
    } 
?> 
+0

表明您生成表單的代碼。 – 2012-03-19 09:06:33

回答

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

非常感謝。這一個工作。但是對於日期字段它不起作用。 – harismahesh 2012-03-19 09:25:43

+0

結束日期:」>> harismahesh 2012-03-19 09:47:28

+0

這是我的日期提交選擇器的代碼。請幫忙 – harismahesh 2012-03-19 09:47:48

相關問題