繁体   English   中英

将多行数据插入从另一个表获取的数据库中

[英]insert multiple rows data into database fetched from another table

我正在尝试在php中建立一个在线出席者,我正在从学生表中获取学生列表,并且想要将所有chkboxs都选中(如果选中),如果未选中,则无法这样做。

<div class="attendance">
    <form accept="att.php" method="POST">
            <?php 
            $sel_sql = "SELECT * FROM student";
            $run_sql = mysqli_query($conn,$sel_sql);
            while($rows = mysqli_fetch_assoc($run_sql)){
                $id = $rows['id'];
                echo '<input type="checkbox" name="status" value="'.$id.'" checked>'.ucfirst($rows['f_name']).'';
            }
        ?>


        <input type="submit" name="submit_attendance" value="Post Attendance">
        <?php echo $msg; ?>
    </form>
</div>

它显示了完美的学生列表,但我不知道如何为所有这些chkbox设置插入查询

尝试此查询从另一个表插入

SELECT * INTO TABLE2 FROM student

使用学生表上的where条件作为where student.column值来选择选中的值

将上述内容与复选框输入字段一起应用

echo '<input type="checkbox" name="status" value="'.$id.'" 'if($row['present_field_of_database']) ? 'checked' : ''
'>'.ucfirst($rows['f_name']).'';

没问题,然后用您的问题更新代码,希望对您有用

    <div class="attendance">
        <form accept="att.php" method="POST">
                <?php 
                    $sel_sql = "SELECT * FROM student";
                    $run_sql = mysqli_query($conn,$sel_sql);


                    while($rows = mysqli_fetch_assoc($run_sql)){
                        $id = $rows['id'];
                        // if your $id value is right from $rows['id'] then
                       // change your student table name to the another table where available status of the student
                        $wh_sql = "SELECT * FROM student WHERE id=".$id;
                        $run_wh_sql = mysqli_query($conn, $wh_sql);
                        $Wh_rows = mysqli_fetch_assoc($run_wh_sql);
                        // add student present or absent value to the rows data
                        $rows['status'] = $Wh_rows['status'];
                    }
                        // set value as A or P respectively absent or present add jquery plugins for onclick event while client click on checkbox change value respectively
                        echo '<input type="checkbox" name="status" value="'.$rows['status'].'" 'if($rows['status'] == "A") ?'checked': '' '>'.ucfirst($rows['f_name']).' onclick= "$(this).val(this.checked ? P : A)"';
                ?>

            <input type="submit" name="submit_attendance" value="Post Attendance">
            <?php echo $msg; ?>
        </form>
    </div>
if (isset($_POST['send'])) {
        $s_id = $_POST['status'];
        $id = $_POST['student'];
        if ($s_id) {
            foreach ($s_id as $s) {
                foreach ($id as $i) {
                        if (mysqli_query($conn, "INSERT INTO attendence(s_id,status) VALUES('".
                        mysqli_real_escape_string($conn, $s)."','".
                        mysqli_real_escape_string($conn, $i)."')")) {
                            $msg =  "success";
                        }else{
                            $msg =  "failed";
                        }
                    }   


            }
        }
    }

我有3个学生。 当我按发送它发送9个条目。 我无法理解如何插入所有学生数据

这是出勤表

这是考勤表

我想放置这样的条目,如果复选框将其贴在p上,如果没有,它将贴在a上。 我只需要如何立即插入所有sutdent

暂无
暂无

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

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