duanpoqiu0919 2015-07-19 10:55
浏览 81

选择带有空白文本框的标签

I have done a form which retrieves data from the database. This form allows you to display the list of students from the database but the problem is I cannot correctly input their scores individually. It does allow to insert scores but unfortunately, it only accepts the last score from the last student and inputs the same score to all students.

These are my codes:

FORM code

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>

This is where I insert my code into the database

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>';
            } 

It should display list students (which is fine) and right beside their name is a blank space that will accept their score (which doesn't work).

  • 写回答

2条回答 默认 最新

  • dongqieli4164 2015-07-19 10:57
    关注

    Because <form name="result" method="post"> is inside the while loop, you actually have multiple forms in your page. The submit button acts only on the last form, that's why only the last student's score gets updated.

    Also, because there are multiple input elements with the same name, only a single value is sent in the POST request. A solution for this would be to include the student's ID in the name:

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

    You need to update the form processing logic accordingly.

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用