2012-03-19 7 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

はフォームを生成するコードを示しています。つまり

は、あなたのような何かをしたいです。 –

答えて

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

+0

終了日:<入力タイプ= "テキスト"サイズ= "25"入力スタイル= "高さ:25px;フォントサイズ:13;"名前= "datum2"値= "<?php echo $ dataum2;?>"> harismahesh

+0

これは、日付ファイルのピッカーのコードです。助けてください – harismahesh

関連する問題