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

时间:2016-12-09 04:24:57

标签: php mysqli

我正在尝试在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设置插入查询

5 个答案:

答案 0 :(得分:0)

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

SELECT * INTO TABLE2 FROM student

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

答案 1 :(得分:0)

使用复选框输入字段

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

答案 2 :(得分:0)

没关系,然后更新您的问题代码我希望它能为您服务

    <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>

答案 3 :(得分:0)

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个条目。我无法理解如何插入所有学生数据

答案 4 :(得分:-1)

This is attendance table

This is attandance table

我想把这样的条目放入,如果复选框chekd它将发布p,如果不是它将发布一个。我只需要如何插入所有sutdent一次quert