while循环中的单选按钮

时间:2014-10-23 08:37:25

标签: 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());

        }

    }

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

1 个答案:

答案 0 :(得分:0)

我不确定但是尝试添加&#39;已检查&#39;无线电输入中的属性

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

<强>
使用选择标签而不是单选按钮

修改
  使用此代码插入当前列或缺少列

$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());