Php studentID的查询插入NULL
问题Php studentID的查询插入NULL,php,html,mysql,Php,Html,Mysql,问题 <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">
<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 -->
我有这个表格,管理员可以输入一个领导id和最多6个学生id。当管理员单击submit按钮时,my PHP文件应该验证输入中的信息,然后在db中输入领导id和学生id。但是当我测试程序时,程序会为学生id插入NULL
团队表(开始时为空)
PHP代码
<?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");
}
?>
// insert them into the database
$insert = $link->prepare("INSERT INTO teams (leaderID, studentID)
VALUES (:leaderID, :studentID)");
$insert->execute(array(
"leaderID" => $leaderID,
"studentID" => $student,
));
我的问题是变量$studentID
未定义,所以我应该使用$student
代码
<?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");
}
?>
// insert them into the database
$insert = $link->prepare("INSERT INTO teams (leaderID, studentID)
VALUES (:leaderID, :studentID)");
$insert->execute(array(
"leaderID" => $leaderID,
"studentID" => $student,
));
fetch
将值提取到某个变量,我假设,第二,您的占位符:student'
将不起作用。您应该会得到一大堆未定义的索引通知。您使用的变量$studentID
从未定义过,这就是它插入NULL的原因。您可能需要$student
。连字符!=下划线和vice versaI发现很难相信这是代码中唯一的错误。