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.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线