从输入获取数据时重复从pdo中的数组插入时循环中的混乱[]

siotufzp  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(278)

作为学校的职责,有一个特殊的出勤和缺勤项目
我创建了一个表单,通过使用[]将其输入表单数据库git到git数组
一切正常,但:

当我从表单中插入数据时,我找不到正确的方法使其正常工作

我曾经循环for以转到下一个索引,foreach以获取单选按钮的索引
这是我的表格我有它的表格数据库:

<br>
<div class="row  justify-content-md-center">
    <h2>شاشة ادخال الحضور والغياب للطالب </h2>
    <br><br>
</div>
<br><br>

<form action="" dir="rtl" method="POST">
    <fieldset>
        <legend>اختيار بيانات التحظير :</legend>
        <div class="row">
            <div class="col">
                <select class="form-control" name="phase_student">
                    <option value="">المرحلة</option>
                    <?php
                    $QueryDB = "SELECT * FROM `phase_student`";
                    $DoQuery = mysqli_query($Con, $QueryDB);
                    $ChekRows = mysqli_num_rows($DoQuery);

                    if ($ChekRows > 0) {
                        while ($Rows = mysqli_fetch_assoc($DoQuery)) {
                            echo "<option value='" . $Rows['num_phase'] . "'> " . $Rows['phase'] . " </option>";
                        }
                    } else {
                        echo "<option value=''>لا توجد بيانات مرحلة   </option>";
                    } ?>
                </select>
            </div>
            <div class="col">

                <select class="form-control" name="classe">
                    <option value="">الصف</option>
                    <?php

                    $QueryDB2 = "SELECT * FROM `classe`";
                    $DoQuery2 = mysqli_query($Con, $QueryDB2);
                    $ChekRows2 = mysqli_num_rows($DoQuery2);

                    if ($ChekRows2 > 0) {
                        while ($Rows2 = mysqli_fetch_assoc($DoQuery2)) {
                            echo "<option value='" . $Rows2['num_class'] . "'> " . $Rows2['class'] . " </option>";
                        }
                    } else {
                        echo "<option value=''>لا توجد بيان  الصفوف  </option>";
                    } ?>

                </select>
            </div>
            <div class="col">

                <select class=" form-control" name="prong">
                    <option value="">الشعبة</option>
                    <?php

                    $QueryDB3 = "SELECT * FROM `prong`";
                    $DoQuery3 = mysqli_query($Con, $QueryDB3);
                    $ChekRows3 = mysqli_num_rows($DoQuery3);

                    if ($ChekRows3 > 0) {
                        while ($Rows3 = mysqli_fetch_assoc($DoQuery3)) {
                            echo "<option value='" . $Rows3['num_prong'] . "'> " . $Rows3['prong'] . " </option>";
                        }
                    } else {
                        echo "<option value=''>لا توجد بيان  شعب   </option>";
                    } ?>
                </select>
            </div>

        </div>

        <br>
        <div class="row">
            <div class="col">
                <select class="form-control" name="item">
                    <option value="">اسم المادة</option>
                    <?php

                    $QueryDB6 = "SELECT * FROM `item`";
                    $DoQuery6 = mysqli_query($Con, $QueryDB6);
                    $ChekRows6 = mysqli_num_rows($DoQuery6);

                    if ($ChekRows6 > 0) {
                        while ($Rows6 = mysqli_fetch_assoc($DoQuery6)) {
                            echo "<option value='" . $Rows6['num_item'] . "'> " . $Rows6['item'] . " </option>";
                        }
                    } else {
                        echo "<option value=''>لا توجد بيان المواد    </option>";
                    } ?>
                </select>
            </div>
            <div class="col">

                <select class=" form-control" name="monthe">
                    <option value="">الشهر</option>
                    <?php

                    $QueryDB5 = "SELECT * FROM `monthe`";
                    $DoQuery5 = mysqli_query($Con, $QueryDB5);
                    $ChekRows5 = mysqli_num_rows($DoQuery5);

                    if ($ChekRows5 > 0) {
                        while ($Rows5 = mysqli_fetch_assoc($DoQuery5)) {
                            echo "<option value='" . $Rows5['num_month'] . "'> " . $Rows5['monthe'] . " </option>";
                        }
                    } else {
                        echo "<option value=''>لا توجد بيان  الاشهر   </option>";
                    } ?>
                </select>
            </div>

            <div class="col">

                <input type="date" class="form-control" name="thisDay" value="">
            </div>

        </div>
    </fieldset>

    <br>
    <br>

    <div class="container">

        <table class="table table-bordered">
            <thead>
            <tr>
                <th>متسلسل</th>
                <th>اسم الطالب</th>
                <th>حاضر</th>
                <th>غائب</th>
                <th>مستئذن</th>
                <th> ملاحظات</th>

            </tr>
            </thead>
            <tbody>
            <tr>
                <?php

                $queryStu = "SELECT * FROM `student_academia` ";
                $DoQuerystu = mysqli_query($Con, $queryStu);
                $ChekRowsstu = mysqli_num_rows($DoQuerystu);

                if ($ChekRowsstu > 0) {
                    while ($Rowsstu = mysqli_fetch_assoc($DoQuerystu)) {
                        echo " <td> <span> " . $Rowsstu['num__student'] . "</span> <input type='hidden' name ='IdStu[]' value='" . $Rowsstu['num__student'] . "' > </td>";
                        echo " <td> <span> " . $Rowsstu['name_student'] . "</span> <input type='hidden' name ='NaStu[]' value='" . $Rowsstu['name_student'] . "' > </td>";
                        echo "  <td>
                    <div class=\"form-check\">
                        <label class=\"form-check-label\">
                            <input class=\"form-check-input\" type=\"radio\" name=\"attend[" . $Rowsstu['num__student'] . "]\" value=\"1\" id=" . $Rowsstu['num__student'] . ">
                        </label>
                    </div>
                </td>
                <td>
                    <div class=\"form-check\">
                        <label class=\"form-check-label\">
                            <input class=\"form-check-input\" type=\"radio\" name=\"attend[" . $Rowsstu['num__student'] . "]\" value=\"0\"  id=" . $Rowsstu['num__student'] . ">
                        </label>
                    </div>
                </td>
                <td>
                    <div class=\"form-check\">
                        <label class=\"form-check-label\">
                            <input class=\"form-check-input\" type=\"radio\" name=\"excusable[" . $Rowsstu['num__student'] . "]\" value=\"1\"  id=" . $Rowsstu['num__student'] . ">
                            <input class=\"form-check-input sr-only\" type=\"radio\" name=\"excusable[" . $Rowsstu['num__student'] . "]\" value=\"0\"  id=" . $Rowsstu['num__student'] . " checked >
                        </label>
                    </div>
                </td> 
                  <td><input type='text' class='form-control' name='attendNote[]' placeholder='ملاحظات ' /></td>
            </tr>
                ";
                    }
                } else {
                    echo "لا توجد بيان  طلاب";
                }

                ?>

            </tbody>

        </table>
    </div>

    <br>
    <br>

    <div class=" row justify-content-center">
        <div class="col-3">
            <input class="btn btn-outline-primary btn-block" type="submit" name="savedata"
                   value="حفظ"/>
        </div>
        <div class="col-3">
            <input class="btn  btn-block  btn-outline-warning  " type="reset" value="مسح"/>
        </div>
        <div class="col-3">
            <button type="button" class="btn  btn-block btn-outline-danger" name="close">خروج
            </button>
        </div>
    </div>
</form>

这是我的表格:

<?php
include "header.php";
//connect to data base oraginal con
include "ConDB.php";
// connect to database but Php Data Opject conect
$db = new PDO("mysql:host=localhost;dbname=school_db", "root", "");
// utf8 supurt arabic
$db->exec("SET CHARACTER SET utf8");
// show error on connect
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// when some body click save button
if (isset($_POST['savedata'])) {

    $phase_student = $_POST['phase_student'];
    $classe = $_POST['classe'];
    $prong = $_POST['prong'];
    $item = $_POST['item'];
    $monthe = $_POST['monthe'];
    $thisDay = $_POST['thisDay'];
//array
    $IdStu = $_POST['IdStu'];
    $NaStu = $_POST['NaStu'];
    $attends = $_POST['attend'];
    $excusables = $_POST['excusable'];
    $attendNote = $_POST['attendNote'];

    echo "<br>";
    var_dump($NaStu);

    echo "<br>";
    var_dump($IdStu);
    echo "<br>";
    var_dump($attends);
    echo "<br>";
    var_dump($attendNote);
    echo "<br>";
    var_dump($excusables);

    echo "<br>";
    /*
               $queryInsertAttend = "INSERT INTO `outcom_month`(`num__student`, `name_student`, `num_month`, `num_class`,`class`, `prong`, `num_item`, `item`, `duties(20)`, `oral(20)`, `assiduity(20)`, `edit(40)`, `total`, `tribnte`, `note_avant`, `note_guardian`) VALUES (
       $num__student ,$num__student, $num_month ,$num_class,$num_class,$prong,$num_item,$num_item,
       $duties,$oral,$assiduity,$edit,$edit,$tribnte,$note_avant,$note_avant)";

     **/
//
//        $id = new ArrayIterator($IdStu);
//        $at = new ArrayIterator($attend);
//        $atnot = new ArrayIterator($attendNote);
//
//        $dataa = new MultipleIterator(MultipleIterator::MIT_NEED_ALL);
//
//        $dataa->attachIterator($id);
//        $dataa->attachIterator($at);
//        $dataa->attachIterator($atnot);
////
    if (!empty($_POST)) {

        //$insert_stu_Attend = $db->prepare("insert into test  values (?,?,?,?,?,?,?,?)");
        $insert_stu_Attend = $db->prepare("insert into test2  values (?,?,?,?,?,?,?,?)");
        $insert_stu_Attend->bindParam(1, $first);
        $insert_stu_Attend->bindParam(2, $second);
        $insert_stu_Attend->bindParam(3, $third);
        $insert_stu_Attend->bindParam(4, $forth);
        $insert_stu_Attend->bindParam(5, $fifth);
        $insert_stu_Attend->bindParam(6, $sixth);
        $insert_stu_Attend->bindParam(7, $seventh);
        $insert_stu_Attend->bindParam(8, $eighth);

        for ($i = 0; $i < 3; $i++) {
            foreach ($IdStu as $s) {
                $dd = 1;
                $first = $IdStu[$i];
                $second = $monthe;
                $third = $item;
                $forth = $classe;
                $fifth = $thisDay;
                $sixth = $attends[$s];
                $seventh = $excusables[$s];
                $eighth = $attendNote[$i];
                $insert_stu_Attend->execute();
            }
        }
    }

} else {
    echo '<br>';
    echo " لم يتم الادخال";
}

?>

每次我拿到桌上唯一的第一排。
选项卡定义:

CREATE TABLE test (
    num__student int(11) NOT NULL UNIQUE,
    year_study date NOT NULL,
    num_item int(11) NOT NULL,
    calss varchar(15) NOT NULL,
    date_day date NOT NULL,
    attendee tinyint(1) NOT NULL,
    excusable tinyint(1) NOT NULL,
    nate mediumtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
vpfxa7rd

vpfxa7rd1#

显然你用的是 bindParam 不是最正确的方式。
据此:https://www.w3schools.com/php/php_mysql_prepared_statements.asp,您应该在循环之前绑定它们,并使用循环仅调整绑定的变量。
我要以你的代码为例,好吗?

if (!empty($_POST)) {
    $insert_stu_Attend = $db->prepare("insert into test  values (?,?,?,?,?,?,?,?)");
    $insert_stu_Attend->bindParam(1, $first);
    $insert_stu_Attend->bindParam(2, $second);
    $insert_stu_Attend->bindParam(3, $third);
    $insert_stu_Attend->bindParam(4, $forth);
    $insert_stu_Attend->bindParam(5, $fifth);
    $insert_stu_Attend->bindParam(6, $sixth);
    $insert_stu_Attend->bindParam(7, $seventh);
    $insert_stu_Attend->bindParam(8, $eighth);

    foreach ($IdStu as $key => $s) {
        $dd = 1;
        $first = $IdStu[$key];
        $second = $monthe;
        $third = $item;
        $forth = $classe;
        $fifth = $thisDay;
        $sixth = $attends[$s];
        $seventh = $excusables[$s];
        $eighth = $attendNote[$key];
        $insert_stu_Attend->execute();
    }
}

根据文件,它应该是有效的。告诉我进展如何。
edit我删除了for,只保留了foreach,因为考虑到为这个结果发送的用户数,我假设您使用for来循环来自不同数组的结果。

e4yzc0pl

e4yzc0pl2#

我决定写一个答案,作为你的指导,尽管我们还有很多未知的东西。您决定如何从 student_academia 把它保存到 test table由你决定。
待办事项
有注解行标记为 @todo . 搜索并取消注解,以查看已发布值的结构外观,例如 $_POST 变量。
在测试我的代码之前,用你的db凭据替换我的db凭据。
一些建议
仅使用pdo扩展。尽管我仍然在代码中使用mysqli(面向对象)和pdo。
将有关从数据库中提取的代码与用于显示提取值的代码分开。e、 g.首先获取并将所有数据(需要在表单中使用的)保存到数组中。然后,在表单代码中,只需遍历这些数组并读取它们的数据。这是一个理想的设计原则,它被称为关注点分离。
不要从php打印html代码。所以不要这样写 echo "<option value='3'>Item 3</option>"; .
原则上,你的代码应该以一种清晰的方式写出来,让其他人很容易理解。以下是基于我的编码方式和我使用的命名约定的一些说明:
表名使用复数形式,如: items , students .
在表中,尝试始终使用表名的单数形式的autoincremental pk(例如主键)列,如:in table itemsitem_id ,在表中 studentsstudent_id . 例如,如果您希望有一个具有唯一索引的列,那么它将是pk列旁边的另一列。例如,在 students 表中有以下两列: student_id 以及 identifier . 注意我用了 identifier 而不是 num__student .
在db表中使用清晰的名称。所以,在db表中使用 year_of_study 而不是 year_study . 或者代替 num_item 使用 item_id ,或 item_identifier .
在php中,使用意图揭示名称。例如 $ChekRows = mysqli_num_rows(...); 使用 $numberOfRows = mysqli_num_rows(...); . 或者,代替 $IdStu 以及 $NaStu ,使用 $studentIdentifier 以及 $studentName . 或者,作为函数名,而不是 getStudents($id) 使用 getStudentsById($id) .
以下是一些很好的资源:
干净、高质量的代码
来自php标准建议的psr 1、psr 2、psr 4
治疗妄想
清洁代码三:功能-罗伯特c。马丁和一个书面的简短版本
祝你好运。
索引.php

<?php
require 'header.php';
require 'connection_mysqli.php';
require 'connection_pdo.php';

/*
 * Signalize if the posted data was sucessfully saved in the "test" table,
 * so that a message is displayed after the save operation is finished.
 */
$dataSaved = FALSE;

// Operations upon form submission.
if (isset($_POST['submit'])) {
    /*
     * @todo Uncomment this line to see how the structure of the posted values looks like.
     */
    // echo '<pre>' . print_r($_POST, TRUE) . '</pre>';

    // Get the values from the options list.
    $phaseStudent = empty($_POST['phaseStudent']) ? NULL : $_POST['phaseStudent'];
    $class = empty($_POST['class']) ? NULL : $_POST['class'];
    $prong = empty($_POST['prong']) ? NULL : $_POST['prong'];
    $item = empty($_POST['item']) ? NULL : $_POST['item'];
    $monthe = empty($_POST['monthe']) ? NULL : $_POST['monthe'];
    $thisDay = empty($_POST['thisDay']) ? NULL : $_POST['thisDay'];

    // Get the values from the students list.
    $studentIds = $_POST['studentId'] ?? [];
    $studentNames = $_POST['studentName'] ?? [];
    $attends = $_POST['attend'] ?? [];
    $excusables = $_POST['excusable'] ?? [];
    $attendNotes = $_POST['attendNote'] ?? [];

    // Save the values for each student.
    if ($studentIds) {
        foreach ($studentIds as $key => $studentId) {
            /*
             * Check if a record with the posted student
             * id already exists in the "test" table.
             */
            $sql = 'SELECT COUNT(*)
                        FROM test
                        WHERE num__student = :num__student';

            $statement = $pdo->prepare($sql);

            $statement->execute([
                ':num__student' => $studentId,
            ]);

            $numberOfFoundRecords = $statement->fetchColumn(0);

            /*
             * If a record with the posted student id already exists in the
             * "test" table, then update it. Otherwise insert a new record.
             */
            if ($numberOfFoundRecords > 0) { // Record exists already. So update.
                $sql = 'UPDATE test
                        SET
                            year_study = :year_study,
                            num_item = :num_item,
                            class = :class,
                            date_day = :date_day,
                            attendee = :attendee,
                            excusable = :excusable,
                            note = :note
                        WHERE num__student = :num__student';

                $statement = $pdo->prepare($sql);

                $statement->execute([
                    ':year_study' => $monthe,
                    ':num_item' => $item,
                    ':class' => $class,
                    ':date_day' => $thisDay,
                    ':attendee' => array_key_exists($studentId, $attends) ? $attends[$studentId] : 0,
                    ':excusable' => array_key_exists($studentId, $excusables) ? 1 : 0,
                    ':note' => empty($attendNotes[$studentId]) ? NULL : $attendNotes[$studentId],
                    ':num__student' => $studentId,
                ]);
            } else {// Record does not exist. So insert.
                $sql = 'INSERT INTO test (
                            num__student,
                            year_study,
                            num_item,
                            class,
                            date_day,
                            attendee,
                            excusable,
                            note
                        ) VALUES (
                            :num__student,
                            :year_study,
                            :num_item,
                            :class,
                            :date_day,
                            :attendee,
                            :excusable,
                            :note
                        )';

                $statement = $pdo->prepare($sql);

                $statement->execute([
                    ':num__student' => $studentId,
                    ':year_study' => $monthe,
                    ':num_item' => $item,
                    ':class' => $class,
                    ':date_day' => $thisDay,
                    ':attendee' => array_key_exists($studentId, $attends) ? $attends[$studentId] : 0,
                    ':excusable' => array_key_exists($studentId, $excusables) ? 1 : 0,
                    ':note' => empty($attendNotes[$studentId]) ? NULL : $attendNotes[$studentId],
                ]);
            }
        }

        $dataSaved = TRUE;
    }
}

// Fetch the phase students.
$sql = 'SELECT * FROM phase_student';
$result = $mysqli->query($sql);
$phaseStudents = $result->fetch_all(MYSQLI_ASSOC);
$result->close();

// Fetch the classes.
$sql = 'SELECT * FROM classe';
$result = $mysqli->query($sql);
$classes = $result->fetch_all(MYSQLI_ASSOC);
$result->close();

// Fetch the prongs.
$sql = 'SELECT * FROM prong';
$result = $mysqli->query($sql);
$prongs = $result->fetch_all(MYSQLI_ASSOC);
$result->close();

// Fetch the items.
$sql = 'SELECT * FROM item';
$result = $mysqli->query($sql);
$items = $result->fetch_all(MYSQLI_ASSOC);
$result->close();

// Fetch the months.
$sql = 'SELECT * FROM monthe';
$result = $mysqli->query($sql);
$monthes = $result->fetch_all(MYSQLI_ASSOC);
$result->close();

// Fetch the student academias.
$sql = 'SELECT * FROM student_academia ORDER BY name_student ASC';
$result = $mysqli->query($sql);
$students = $result->fetch_all(MYSQLI_ASSOC);
$result->close();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Demo</title>

        <!-- CSS assets -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">

        <!-- JS assets -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

        <style type="text/css">
            body {
                padding: 20px;
            }

            .page-title {
                margin-bottom: 20px;
            }

            .options-list {
                margin-bottom: 30px;
            }

            .students-list {
                margin-bottom: 20px;
            }
        </style>
    </head>
    <body>

        <div class="row justify-content-md-center">
            <h2 class="page-title">شاشة ادخال الحضور والغياب للطالب </h2>
        </div>

        <form action="" dir="rtl" method="POST">
            <?php
            if ($dataSaved) {
                ?>
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    Data successfully saved
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <?php
            }
            ?>

            <fieldset class="options-list">
                <legend>اختيار بيانات التحظير :</legend>
                <div class="row">
                    <div class="col">
                        <select class="form-control" name="phaseStudent">
                            <option value="">- Select a phase student -</option>
                            <?php
                            if ($phaseStudents) {
                                foreach ($phaseStudents as $phaseStudent) {
                                    ?>
                                    <option value="<?php echo $phaseStudent['num_phase']; ?>">
                                        <?php echo $phaseStudent['phase']; ?>
                                    </option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col">
                        <select class="form-control" name="class">
                            <option value="">- Select a class -</option>
                            <?php
                            if ($classes) {
                                foreach ($classes as $class) {
                                    ?>
                                    <option value="<?php echo $class['num_class']; ?>">
                                        <?php echo $class['class']; ?>
                                    </option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col">
                        <select class="form-control" name="prong">
                            <option value="">- Select a prong -</option>
                            <?php
                            if ($prongs) {
                                foreach ($prongs as $prong) {
                                    ?>
                                    <option value="<?php echo $prong['num_prong']; ?>">
                                        <?php echo $prong['prong']; ?>
                                    </option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="col">
                        <select class="form-control" name="item">
                            <option value="">- Select an item -</option>
                            <?php
                            if ($items) {
                                foreach ($items as $item) {
                                    ?>
                                    <option value="<?php echo $item['num_item']; ?>">
                                        <?php echo $item['item']; ?>
                                    </option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col">
                        <select class="form-control" name="monthe">
                            <option value="">- Select a monthe -</option>
                            <?php
                            if ($monthes) {
                                foreach ($monthes as $monthe) {
                                    ?>
                                    <option value="<?php echo $monthe['num_month']; ?>">
                                        <?php echo $monthe['monthe']; ?>
                                    </option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col">
                        <input type="date" class="form-control" name="thisDay">
                    </div>
                </div>
            </fieldset>

            <div class="container students-list">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Number Stu</th>
                            <th>Name Stu</th>
                            <th>attends</th>
                            <th>absent</th>
                            <th>excusable</th>
                            <th>note</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if ($students) {
                            foreach ($students as $student) {
                                ?>
                                <tr>
                                    <td>
                                        <span>
                                            <?php echo $student['num__student']; ?>
                                        </span>
                                        <input type="hidden" name="studentId[]" value="<?php echo $student['num__student']; ?>">
                                    </td>
                                    <td>
                                        <span>
                                            <?php echo $student['name_student']; ?>
                                        </span>
                                        <input type="hidden" name="studentName[<?php echo $student['num__student']; ?>]" value="<?php echo $student['name_student']; ?>">
                                    </td>
                                    <td>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="radio" name="attend[<?php echo $student['num__student']; ?>]" id="attends<?php echo $student['num__student']; ?>" value="1">
                                            </label>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="radio" name="attend[<?php echo $student['num__student']; ?>]" id="absent<?php echo $student['num__student']; ?>" value="0">
                                            </label>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="form-check">
                                            <label class="form-check-label">
                                                <input class="form-check-input" type="checkbox" name="excusable[<?php echo $student['num__student']; ?>]">
                                            </label>
                                        </div>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" name="attendNote[<?php echo $student['num__student']; ?>]" />
                                    </td>
                                </tr>
                                <?php
                            }
                        } else {
                            ?>
                            <tr>
                                <td colspan="6">
                                    No students found
                                </td>
                            </tr>
                            <?php
                        }
                        ?>
                    </tbody>
                </table>
            </div>

            <div class="row justify-content-center">
                <div class="col-3">
                    <button type="submit" name="submit" value="save" class="btn btn-block btn-outline-primary">
                        Save
                    </button>
                </div>
                <div class="col-3">
                    <button type="reset" name="reset" value="reset" class="btn btn-block btn-outline-warning">
                        Reset
                    </button>
                </div>
                <div class="col-3">
                    <button type="button" name="close" value="close" class="btn btn-block btn-outline-danger">
                        Close
                    </button>
                </div>
            </div>
        </form>

    </body>
</html>

连接\u mysqli.php

<?php

// This page contains the code for creating a mysqli connection instance.

/*
 * Enable internal report functions. This enables the exception handling,
 * e.g. mysqli will not throw PHP warnings anymore, but mysqli exceptions
 * (mysqli_sql_exception).
 */
$mysqliDriver = new mysqli_driver();
$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Create a db connection.
$mysqli = new mysqli('localhost', 'root', 'root', 'tests', 3306);

连接\u pdo.php

<?php

// This page contains the code for creating a PDO connection instance.

$pdo = new PDO(
        'mysql:host=localhost;port=3306;dbname=tests;charset=utf8'
        , 'root'
        , 'root'
        , [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => TRUE,
    PDO::ATTR_PERSISTENT => FALSE,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]
);

表格定义-详细阅读!

CREATE TABLE `test` (
  `num__student` int(11) unsigned NOT NULL,
  `year_study` date DEFAULT NULL,
  `num_item` int(11) DEFAULT NULL,
  `class` varchar(15) DEFAULT NULL,
  `date_day` date DEFAULT NULL,
  `attendee` enum('0','1') NOT NULL DEFAULT '0',
  `excusable` enum('0','1') NOT NULL DEFAULT '0',
  `note` text,
  PRIMARY KEY (`num__student`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `student_academia` (
  `num__student` int(11) unsigned NOT NULL,
  `name_student` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`num__student`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

相关问题