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

标签 php html mysql forms textbox

我已经完成了一个从数据库检索数据的表单。此表单允许您显示数据库中的学生列表,但问题是我无法正确地单独输入他们的分数。它确实允许插入分数,但不幸的是,它只接受最后一个学生的最后分数,并向所有学生输入相同的分数。

这些是我的代码:

表格代码

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

它应该显示学生列表(这很好),并且他们的名字旁边有一个空白区域,可以接受他们的分数(这不起作用)。

最佳答案

因为<form name="result" method="post">在 while 循环内,您的页面中实际上有多个表单。提交按钮仅对最后一个表单起作用,这就是为什么只有最后一个学生的分数会被更新。

另外,因为有多个input具有相同 name 的元素,POST 请求中仅发送单个值。解决方案是在姓名中包含学生 ID:

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

您需要相应地更新表单处理逻辑。

关于php - 选择带有空白文本框的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500402/

相关文章:

php - 如何在 PHP 中仅显示选定的行?

html - 2 使用 HTML 和 CSS 可变宽度的列

mysql - 如何连接三个表并在 GridView 中获取值

html - 增加 HTML 中复选框的大小

mysql - SQL 通过限定多个字符串进行选择

mysql - 根据上次修改的属性排序时出现分页问题

php:生成 MySQL 数据库列中尚未存在的 5 位数字的最快方法(具有唯一属性)

php - 使用 fsockopen 重写一些代码以使用 curl 代替

php - 形成对具有属性的 url 的操作

php - 如何使用html和php上传音频文件?