php - while循环中的单选按钮

标签 php html mysql

我正在编写一个用于学生出勤的页面,在该页面中,所有学生的姓名都是使用 while 循环从数据库中动态检索的,在 while 循环内我使用了一个单选按钮。

我的问题是单选按钮工作正常,但我无法将单选按钮的值插入数据库。这是我的代码:

include'connection.php';
@$id = mysql_real_escape_string($_GET['id']);
$res=mysql_query("SELECT * FROM `std_register` AS p1 ,`sims-register-course` AS p2 WHERE p2.semester=p1.std_semester and p2.department=p1.std_department and p2.id = '".$id."'");
if ($res==FALSE) {
echo die(mysql_error());
                }
while ($row=mysql_fetch_assoc($res)) { ?>

    <input type="hidden" id="dept_0"            name="dept[]"           value="<?php echo $row["department"] ?>">
    <input type="hidden" id="course_name_1"     name="course_name[]"    value="<?php echo $row["course_name"]  ?>">
    <input type="hidden" id="std_semester_2"    name="std_semester[]"   value="<?php echo $row["semester"]  ?>">
    <input type="hidden" id="std_id_3"          name="std_id[]"         value="<?php echo $row["id"]  ?>">
    <input type="hidden" id="std_name_4"        name="std_name[]"       value="<?php echo $row["std_name"]  ?>">
    <tr>
    <td class =info><?php echo $row["std_name"];?></td>
    <td><input type="radio"             name="<?php  echo "status['".$row["std_name"]."']" ?>"  value="1"></td>
    <td><input type="radio"             name="<?php  echo "status['".$row["std_name"]."']" ?>"  value="2"></td>
     </tr>

    <?php   }?>
    </table><br>

    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-3">
    <input type="submit" name="submit" value="Assign" class="btn btn-large btn-block btn-primary"></div></div>
    </form>

    <?php 
    for ($i=0; $i < 4; $i++) {
    if(isset($_POST["submit"])) {
    $stdid = mysql_real_escape_string($_POST['std_id'][$i]);
    $dptname = mysql_real_escape_string($_POST['dept'][$i]);
    $courseName = mysql_real_escape_string($_POST['course_name'][$i]);
    $stdSemester = mysql_real_escape_string($_POST['std_semester'][$i]);
    $std_name = mysql_real_escape_string($_POST['std_name'][$i]);
    $present = mysql_real_escape_string($_POST['status'][$i]);

    $totalClasses = $present;                                            
    $insrt = mysql_query("INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester','$present',0,0)") or die(mysql_error());

        }

    }

是否可以将此单选按钮的值插入到数据库的两列中?

最佳答案

我不确定,但尝试在单选输入中添加“选中”属性

<input type="radio" name="<?php  echo "status[".$row["std_name"]."]" ?>"  value="1" checked></td>

或者
使用Select标签代替单选按钮

编辑
使用此代码插入当前列或不存在的列

$value = mysql_real_escape_string($_POST['status'][$i]);
$totalClasses = $value;
if($value == 1) //check if present
{
$sql="INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester','$value',0,0)";
}
else  //if absent
{
$sql="INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester',0,'$value',0)";
}

$insrt = mysql_query($sql) or die(mysql_error());

关于php - while循环中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524236/

相关文章:

mysql - 连接两个表,不同日期频率的MySQL

mysql - 使用 JPA/Spring/Hibernate 自动截断字符串列的方法?

javascript - 使用无限 Ajax 滚动时的 jQuery 函数

PHP 在包含 html 文件之前添加空文本节点

javascript - onmouseover 覆盖了 :hover in CSS

php - 如何使用php获取同一页面中的输入数据

javascript - 自动刷新文本框值

mysql - 重启服务器后无法启动mysql

php - 如果字符串不是链接,则正则表达式替换字符串中的单词

php - 调用资源上的成员函数 fetch_object()