0

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.

1 Answers1

0

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,
            ));