matlabmann 2014-10-23 08:37
浏览 105
已采纳

while循环中的单选按钮

i am coding a page for the attendance of students and in the page all the students name are retrieved dynamically from database using while loop and inside the while loop i have used a radio button.

my problem is the radio button work's correctly but i am unable to insert the value of radio buttons into database.here is my code :

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

        }

    }

and is it possible to insert values from this radio buttons to two columns of database?

  • 写回答

1条回答 默认 最新

  • douyi2107 2014-10-23 09:00
    关注

    I am not sure but try to add 'checked' property in radio input

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

    Or
    use Select tag instead of radio button

    EDIT
    use this code to insert in present column or absent column

    $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());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)