所以,我有一个名为teams
的数据库,它目前看起来像这样:
team_id | leader_id | student_id
1 | 123456 | 1234
2 | 1234 | 123456, 1234567890
3 | 1234 | 123456
4 | 0 | 12345, 1234, 1234567890
5 | 0 |
6 | 0 |
我的students
数据库看起来像这样
student_id | first_name | last_name
123456 | qwer | qwer
0 | asd | zxc
1234567890 | mmm |nnn
用户输入leader_id
,用户最多可输入7 student_id
。问题是,如果用户输入1 student_id
,一切正常,但当用户输入的数据超过1 student_id
时,则不止一次存储数据。
示例:
如果用户输入1234
作为leader_id
,并输入123456
和1234567890
作为student_id
,则应将数据存储在一行中,不要将数据分成2行。
另一件事是,在最后3行中,用户输入3 student_id
,数据存储在1行中,但它增加了2行,student_id
我的PHP
<?php
error_reporting(0);
require '../../connect.php';
$leader_id = $_POST['leader_id'];
$student_id = 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']
);
$students_save = array();
if(!empty($leader_id)) {
foreach ($student_id as $id) {
if(!empty($id)) {
array_push($students_save, $id);
}
}
foreach ($students_save as $save) {
$check_id = mysqli_query($link, "SELECT student_id FROM students WHERE student_id = $save");
if(mysqli_num_rows($check_id)>0) {
$students_save = implode(", ", $students_save);
$result = mysqli_query($link, "INSERT INTO teams (leader_id, student_id)
VALUES ('$leader_id', '$students_save');");
if($result) {
header("Location: ../../../admin.php?message=Success!");
} else {
header("Location: ../../../admin.php?message=Sorry we ran into an error");
}
} else {
header("Location: ../../../admin.php?message=A Student ID Doesn't Exist");
}
}
} else if(empty($leader_id)){
header("Location: ../../../admin.php?Please add you're input values");
}
?>
答案 0 :(得分:1)
在编辑问题时,
如果你的结果是$,那么你所需要的只是打破你的foreach循环 successl所以如果($ result)其余的代码将保留,则添加break 相同的
请参阅以下代码
<?php
foreach ($students_save as $save) {
$check_id = mysqli_query($link, "SELECT student_id FROM students WHERE student_id = $save");
if(mysqli_num_rows($check_id)>0) {
$students_save = implode(", ", $students_save);
$result = mysqli_query($link, "INSERT INTO teams (leader_id, student_id)
VALUES ('$leader_id', '$students_save');");
if($result) {
header("Location: ../../../admin.php?message=Success!");
break; // add break here, as if your condition
} else {
header("Location: ../../../admin.php?message=Sorry we ran into an error");
}
} else {
header("Location: ../../../admin.php?message=A Student ID Doesn't Exist");
}
}
答案 1 :(得分:0)
如果有帮助,请尝试以下代码,
<?php
error_reporting(0);
require '../../connect.php';
$leader_id = $_POST['leader_id'];
$student_id = array();
if(isset($_POST['student_id_1'])) $student_id[] = $_POST['student_id_1'];
if(isset($_POST['student_id_2'])) $student_id[] = $_POST['student_id_2'];
if(isset($_POST['student_id_3'])) $student_id[] = $_POST['student_id_3'];
if(isset($_POST['student_id_4'])) $student_id[] = $_POST['student_id_4'];
if(isset($_POST['student_id_5'])) $student_id[] = $_POST['student_id_5'];
if(isset($_POST['student_id_6'])) $student_id[] = $_POST['student_id_6'];
if(!empty($leader_id)) {
$students_save = implode(", ", $student_id);
$check_id = mysqli_query($link, "SELECT student_id FROM students WHERE student_id IN ($students_save)");
if(mysqli_num_rows($check_id)>0) {
//$student_ids = array();
//while($row = $check_id->fetch_array(MYSQLI_ASSOC)) $student_ids[] = $row['student_id'];
//$students_save = implode(", ", $student_ids);
$result = mysqli_query($link, "INSERT INTO teams (leader_id, student_id) VALUES ('$leader_id', '$students_save');");
if($result) {
header("Location: ../../../admin.php?message=Success!");
} else {
header("Location: ../../../admin.php?message=Sorry we ran into an error");
}
} else {
header("Location: ../../../admin.php?message=A Student ID Doesn't Exist");
}
} else if(empty($leader_id)) {
header("Location: ../../../admin.php?Please add you're input values");
}
?>
此代码可以为您提供帮助。
注意:我已对部分代码添加了评论。如果删除评论,则只会插入学生表中存在的student_id。