2015-07-19 25 views
0

我已經完成了從數據庫中檢索數據的表單。這種形式允許你從數據庫中顯示學生名單,但問題是我無法單獨正確輸入他們的分數。它允許插入分數,但不幸的是,它只接受最後一名學生的最後一個分數,並向所有學生輸入相同的分數。選擇帶空白文本框的標籤

這是我的代碼:

FORM代碼

if ($result->num_rows > 0) { // output data of each row 
    while($row = $result->fetch_assoc()) { 
    if($row['status']=='p'){ 


    <form name="result" method="post"> 
    <?php {   //this form will display the set of students 

      echo $row['lastname'] . ', ' . $row['firstname']; 
      echo '<input type="hidden" name="selected[]" value="'.$row['reviewee_idnumber'].'"/>'; ?> 
      <input type="text" name="score" required="required" size="20" placeholder="Score"/><br><br> 
    <?php echo '</br>'; 
     } 


    } //if statement 
    } //while statement 
    ?> 

<input type="submit" name="submit" value="Submit"/> 
<input type="hidden" name="code" value="<?php echo $code;?>"/> 
<input type="hidden" name="subject" value="<?php echo $subject;?>"/> 
<input type="hidden" name="items" value="<?php echo $items;?>"/> 
<input type="hidden" name="date" value="<?php echo $date;?>"/> 
</form> 

這是我插入我的代碼插入到數據庫

if(isset($_POST['submit'])){ 


$code = $_POST['code']; 
$subject = $_POST['subject']; 
$items = $_POST['items']; 
$date = $_POST['date']; 
$score = $_POST['score']; //get score 

$update = mysql_query("INSERT INTO exam (exam_code, subject, date, total_item) 
            VALUE ('$code','$subject', '$date', '$items')"); 


if(!empty($_POST['selected'])) {  
    $checked_count = count($_POST['selected']);// Counting number of checked checkboxes. 
    //echo "You have selected following ".$checked_count." option(s): <br/>"; 

    foreach($_POST['selected'] as $selected){ // Loop to store and display values of individual inputs. 
     $updatedata="INSERT INTO result (score, exam_code, reviewee_idnumber) 
            VALUE ('$score', '$code', '$selected')"; 


      if(@mysql_query($updatedata,$dbc)){ 
       print '<p> successful!</p>'; 
      }else{ 
       print '<p> failed. '.mysql_error().'</p>'; 
      } 

它應該顯示列表中的學生(這是罰款)和在他們的名字旁邊是一個空白空間,將接受他們的分數(這是行不通的)。

回答

0

由於<form name="result" method="post">位於while循環中,因此實際上頁面中有多個表單。提交按鈕僅對最後一個表單起作用,這就是爲什麼只有最後一個學生的分數得到更新。

另外,因爲有多個input元素具有相同的name,所以在POST請求中僅發送一個值。對此的解決方案是在名稱中包含學生的ID:

name="score'.$row['reviewee_idnumber'].'" 

您需要更新相應的表單處理邏輯。

+0

感謝您的回覆。我試圖重新編碼

並將它放在while循環之前,但它不起作用。具有不同reviewee_idnumber的用戶完美地工作,但分數沒有。你還可以建議什麼? – Regina

+0

爲每個輸入字段使用不同的名稱 - 請參閱我更新的答案。 – Glorfindel

+0

雖然仍然無法正常工作,但感謝您的想法!會更深入。乾杯! – Regina

0
name =<?php echo "'score.$row['reviewee_idnumber']'" ?> 
+1

感謝您發佈這個問題的答案! Stack Overflow不鼓勵使用代碼解答,因爲沒有上下文的代碼轉儲並不能解釋解決方案如何或爲什麼會起作用,這使原始海報(或任何未來的讀者)很難理解其背後的邏輯。請編輯你的問題,幷包括你的代碼的解釋,以便其他人可以從你的答案中受益。謝謝! –