【问题标题】:insert multiple rows data into database fetched from another table将多行数据插入从另一个表中获取的数据库中
【发布时间】:2016-12-09 04:24:57
【问题描述】:

我正在尝试在 php 中构建一个在线考勤,我正在从学生表中获取学生列表,并且如果选中为存在并且如果未选中为不存在,则希望放置所有 chkbox, 我无法做到这一点。

<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 设置插入查询

【问题讨论】:

  • 最好给你的表格样本结构
  • 你真正想要什么还不清楚。但现在先说明,如果出勤被标记为 P 则只显示已选中,否则不显示已选中。
  • 我想使用学生表中的学生列表将考勤表放入考勤表。我可以轻松地将用户作为页面上的复选框获取,但我无法将这些学生插入到考勤表中。

标签: php mysqli


【解决方案1】:

试试这个查询从另一个表插入

SELECT * INTO TABLE2 FROM student

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

【讨论】:

  • 你有whatsap吗?请发给我,会发给你详细的吗?这可能吗?
  • 只需在您的问题中更新表结构,您可以获得多个答案并选择最佳答案
  • 已从图片中发布。请检查一下
【解决方案2】:

在复选框输入字段中应用与上述相同的内容

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

【讨论】:

  • 听着,我有两个学生在学生表中,我在一个页面上检索他们作为复选框。现在我想要这些复选框来出席表,我想要插入多行插入的查询。一键式
【解决方案3】:

没关系,然后用你的问题更新代码我希望它对你有用

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

【讨论】:

  • 先生,我没有任何问题要检索,我想一次将所有学生发布到数据库。
【解决方案4】:
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 个条目。我无法理解如何插入所有学生数据

【讨论】:

    【解决方案5】:

    This is attendance table

    This is attandance table

    我想输入这样的条目,如果复选框 chekd 将发布 p,否则将发布 a。我只需要如何一次插入所有 sutdent 查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多