duanji5746 2017-04-15 20:34
浏览 40
已采纳

查询为studentID插入NULL [duplicate]

Problem

I have this form where the admin can enter a leader's id and up to 6 student id's. When the admin click on the submit button, the my PHP file should validate the info in the inputs and then enter the leader's id and the student id(s) in to the db. But when I tested the program, the program inserts NULL for the student id(s).

Teams table (starts off empty)

id | leaderID | studentID

PHP Code

<?php 

require '../connect.php';

$leaderID = $_POST['leader-id'];
$students = array(
    $_POST['student-id-1'],
    $_POST['student-id-2'],
    $_POST['student-id-3'],
    $_POST['student-id-4'],
    $_POST['student-id-5'],
    $_POST['student-id-6'],
);

$studentSave = array();

// check if inputs are not empty
if(!empty($leaderID)) {

    // loop through all students
    foreach ($students as $student) {

        // check if student is not empty
        if(!empty($student)) {
            // add student to new array
            array_push($studentSave, $student);
        }

    }

    // loop through all students
    foreach ($studentSave as $student) {
        // check if students exist
        $getStudent = $link->prepare("SELECT * FROM students
                                    WHERE studentID = ':student'");
        $getStudent->execute(array(
            "student" => $student
        ));
        $getStudent->fetch();

        // if student exist
        if($getStudent) {

            // insert them into the database
            $insert = $link->prepare("INSERT INTO teams (leaderID, studentID)
                                    VALUES (:leaderID, :studentID)");
            $insert->execute(array(
                "leaderID" => $leaderID,
                "studentID" => $studentID,
            ));

            if(!$insert) {
                header("Location: ../../admin.php?msg=Sorry, we ran into an error");
            } else {
                header("Location: ../../admin.php?msgSuccess=Success");
            }

        } else {

            header("Location: ../../admin.php?msg=A student doesn't exist");

        }
    }

} else if(empty($leaderID)){
    header("Location: ../../admin.php?msg=There must be a leader");
}
?>

HTML Form

<div class="add_team">
                <h1>Add Team</h1>

                <form action="server/add/add_team.php" method="post">
                    <input type="text" name="leader_id" placeholder="* Leader ID">
                    <input type="text" name="student_id_1" placeholder="Student ID">
                    <input type="text" name="student_id_2" placeholder="Student ID">
                    <input type="text" name="student_id_3" placeholder="Student ID">
                    <input type="text" name="student_id_4" placeholder="Student ID">
                    <input type="text" name="student_id_5" placeholder="Student ID">
                    <input type="text" name="student_id_6" placeholder="Student ID">

                    <center>
                        <button type="submit" name="Add"><i class="fa fa-plus-circle fa-lg" aria-hidden="true"></i>Add</button>
                    </center>
                </form>
            </div> <!-- END OF .ADD_TEAM -->

So my program loops through all the input values and checks if anyone of the inputs are not empty. Then the program stores the values form the non empty inputs into a new array. The the program checks if the values already exist in the db, and finally queries the data into the db.

</div>
  • 写回答

1条回答 默认 最新

  • dongshen6060 2017-04-15 20:39
    关注

    My problem was that the variable $studentID was undefined, so I should've used $student

    Code

    // insert them into the database
                $insert = $link->prepare("INSERT INTO teams (leaderID, studentID)
                                        VALUES (:leaderID, :studentID)");
                $insert->execute(array(
                    "leaderID" => $leaderID,
                    "studentID" => $student,
                ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源