繁体   English   中英

php-mysql从回声单选按钮插入多行

[英]php-mysql insert multiple rows from echo radio button

实际上,我想开发一个应用程序,用户可以在其中设置学生的出勤率。 因此出席的HTML表单将来自我的数据库查询。 而且它也来了,但问题是我如何将表单信息插入到db中。 实际上我搜寻了很多,但是我没有得到任何理想的结果,我的意思是请任何人可以帮助我。 提前致谢

<form action="attendance.php" method="post"> 
<?php include '../database-config.php';
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){
  echo "<div>   
  <label>".htmlentities($row['student_id'])."</label> 
  <input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present        
  <input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent
        </div></br>"; 
}
?> 
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
<form action="attendance.php" method="post">
    <?php include '../database-config.php';
    $result = mysql_query("SELECT * FROM student WHERE active_class='VII'");
    foreach($result as $row)
    {
        ?>
        <div>
         <label><?php echo $row['student_id']?></label>
         <input type="radio" name="attend" value="present" checked>Present
         <input type="radio" name="attend" value="absent">Absent
        </div>
            </br>
        <?php
    }
    ?>
    <button type="submit" class="btn btn-success btn-lg">Submit</button>
    <button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>

因此在php中您可以像这样获得价值

<?php

$attend = $_POST['attend'];

echo $attend;

?>

因此,在$attend它包含单选按钮的value( value="present" )。

它可能presentabsent

该死的xD很累,但是应该行得通,但是您必须自己将列出席者添加到数据库表中

   <form action="" method="post">
    <?php

    include '../database-config.php';
    if(isset($_POST['attendency']) && isset($_POST['id']))
    {
        $id_to_update = $_POST['id'];
        $status = $_POST['attendency'];
        $ar = array('p','a');
        $attend = !empty($status) && in_array($status,$ar) ? $status : 'p';


        //you have to create a column named attendency for this to work
        $sql = "INSERT INTO student(attendency)  VALUES ('$attend ') WHERE user_id = '$id_to_update '";
        $dbh->query($sql);
}
    foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row)
    {

        if($row['attendency'] == 'p')
        {
            $p = 'checked="checked"';
            $a = '';
        } else {
            $a = 'checked="checked"'
     $p = '';
  } ?>

        <div>
            <input type="hidden" name="id" value="<?=$row['student_id']?>">
            <label><?=$row['student_id']?></label>
            <input type='radio' name='attendency' <?=$p?>>Present
            <input type='radio' name='attendency' <?=$a?>>Absent
        </div></br>

    <?php } ?>

    <button type="submit" class="btn btn-success btn-lg">Submit</button>
    <button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM