使用 PHP 将下拉列表中的值存储到数据库


Storing values from dropdowns to Database using PHP

我正在研究一个考勤模块。最初,它将显示所有学生的列表以及一个下拉列表,其中包含"存在/缺席"选项。教职员工将相应地选择"在场/缺席"并提交相同的内容。

我在将相应的值存储到数据库时遇到问题。

$sql = "select a.student_id, r.student_name, r.section, r.group_name, a.$subject from result.$batch r, attendance.$batch a where a.student_id = r.student_id AND r.section='$section'";
$c   = 1;
$result1 = $result->query($sql);
if ($result1->num_rows > 0) 
{
    while($row = $result1->fetch_assoc()) 
    { 
        echo '<tr>';
        echo "<td>$c </td>";
        echo "<td>{$row['student_id']}</td>";
        echo "<td>{$row['student_name']}</td>";
        echo "<td>{$row['section']}</td>";
        echo "<td>{$row['group_name']}</td>";
        echo "<td>
                  <select class='dropdown' id='attend' name='attend[$c]' > <option value='1'>Present</option> <option value='2'>Absent</option>
              </td>";
        echo '</tr>';
        ++$c;
    }
}
else 
{
    echo "No Results Found";
}

有人可以帮我更新代码吗?更新将在表$batch(批处理是包含要使用的表名的变量)和列$subject(包含变量名)中进行。

好吧,你可以做一件事。当用户单击复选框以标记为不存在或存在时,使用 Javascript 将其保存到数组中,如下所示: onclick='array.push(this.id);'

这会将当前元素的 id 推送到 Javascript 中的数组中。当您最终提交表单时,只需这样做,

onsubmit="passValues();"在脚本标记中,执行此操作

function passValues() { var x = array // the array to which elements were pushed document.getElementById('someBlankElement').innerHTML = "<input type='hidden' value = 'display array here' name='get_this_name_through_php_when_form_submits' >" }

完成了!