条件不适用于 PHP 和 mysqli

Condition not working with PHP and mysqli

提问人:Yerkin 提问时间:4/9/2022 最后编辑:BarmarYerkin 更新时间:4/9/2022 访问量:40

问:

我的数据库中有两个表:学生和教师。我正在尝试为每位老师分配 4 名学生,他们正在就该老师的主题撰写论文。为此,我创建了第三个表:assignment。我提出了两个条件:如果分配了学生(他或她分配的表值为 1),则代码应跳过该学生,如果教师分配的学生超过 3 个(他或她分配的表值小于 4),则代码应跳过该教师。这是我尝试过的代码:

<?php

    require('config_teachers.php');
    require('config_students.php');

    // We get all the info as associative arrays: $students_info and $teachers_info.

    $serverName = "localhost";
    $user = "root";
    $password = "";
    $dbName = "yerkindb";
    
    foreach($students_info as $student_info){
        foreach($teachers_info as $teacher_info){
            if($student_info['assigned'] == 0 && $student_info['ee_subject'] == $teacher_info['ee_subject_1_choice'] && $teacher_info['assigned'] < 4 ){

                $conn = mysqli_connect($serverName, $user, $password, $dbName);

                if(mysqli_connect_errno()){
                        
                    echo "Failed to connect to mysqli.".mysqli_connect_errno();
                        
                } else {
                        
                    $name_student = $student_info['name'];
                    $name_teacher = $teacher_info['name'];
                    $subject = $student_info['ee_subject'];

                    $query = "INSERT INTO assignment(student_name, advisor_name, subject) values('$name_student', '$name_teacher', '$subject')";
                            
                    mysqli_query($conn, $query);

                    $assigned_teacher = $teacher_info['assigned'];

                    $assigned_teacher = $assigned_teacher + 1;

                    $query = "UPDATE teachers SET assigned = $assigned_teacher WHERE name = '$name_teacher'";

                    mysqli_query($conn, $query);

                    $query = "UPDATE students SET assigned = 1 WHERE name = '$name_student'";

                    mysqli_query($conn, $query);

                    mysqli_close($conn);
                }
            }
        }
    }

?>

线

if($student_info['assigned'] == 0 && $student_info['ee_subject'] == $teacher_info['ee_subject_1_choice'] && $teacher_info['assigned'] < 4 ){

具有我想要分配的条件,但是,当我运行代码时,我会让每个学生被分配到与他或她正在撰写论文的主题具有相同主题的每一位老师。

有人可以解释为什么这些条件被跳过了,即使

$student_info['ee_subject'] == $teacher_info['ee_subject_1_choice']

条件不容忽视。

php 数据库 mysqli foreach 条件语句

评论

3赞 Dharman 4/9/2022
警告:您对 SQL 注入持开放态度,并且应该使用参数化的预准备语句,而不是手动构建查询。它们由 PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,您仍然面临数据损坏的风险逃跑是不够的!
0赞 Barmar 4/9/2022
与问题无关:不要每次都通过循环连接到数据库。在开始时建立一种连接并一直使用它。
0赞 Barmar 4/9/2022
$assigned_teacher = $assigned_teacher + 1;不会添加 .因此,这部分条件将始终成功。$teacher_info['assigned']

答:

0赞 Barmar 4/9/2022 #1

将学生分配给教师后,您应该打破教师循环,这样您就不会尝试将学生分配给其他教师。

将学生分配给教师时,需要递增 。为了做到这一点,你的迭代变量需要是对数组元素的引用;否则,您将获得数组的副本,这不会影响未来的迭代。$teacher_info['assigned']

在下面的代码中,我已将所有查询转换为预准备语句。

$conn = mysqli_connect($serverName, $user, $password, $dbName);
if(mysqli_connect_errno()){
    echo "Failed to connect to mysqli.".mysqli_connect_errno();
} else {
    $insert_assignment_stmt = $conn->prepare("INSERT INTO assignment(student_name, advisor_name, subject) values(?, ?, ?)");
    $insert_assignment_stmt->bind_param("sss", $name_student, $name_teacher, $subject);
    $update_teacher_stmt = $conn->prepare("UPDATE teachers SET assigned = assigned + 1 WHERE name = ?");
    $update_teacher_stmt->bind_param("s", $name_teacher);
    $update_student_stmt = $conn->prepare("UPDATE students SET assigned = 1 WHERE name = ?");
    $update_student_stmt->bind_param("s", $name_student);

    foreach($students_info as &$student_info){
        foreach($teachers_info as &$teacher_info){
            if($student_info['assigned'] == 0 && $student_info['ee_subject'] == $teacher_info['ee_subject_1_choice'] && $teacher_info['assigned'] < 4 ){
                $name_student = $student_info['name'];
                $name_teacher = $teacher_info['name'];
                $subject = $student_info['ee_subject'];

                $insert_assignment_stmt->execute();
                $update_teacher_stmt->execute();
                $update_student_stmt->execute();

                $teacher_info['assigned']++;

                break;
            }
        }
    }
}
mysqli_close($conn);

评论

0赞 Yerkin 4/11/2022
你能向我解释一下为什么你在变量 $student_info 和 $teacher_info 之前的 foreach 开场白中使用“&”吗?
0赞 Barmar 4/11/2022
我在回答中解释道——它需要是一个参考变量。查看 stackoverflow.com/questions/8769288/...