dongqiongzheng0615 2017-04-03 23:46
浏览 78
已采纳

查询未插入数据库 - MySQL

Problem

So, the user has to input a team's leader_id, and the user can input up to 6 student_id. My problem is that the data isn't being inserted into the database. The problem is under the comment PROBLEM.

So I first get the users input data, and I store the leader_id inside a separate variable and the student_id's inside an array. Next, I loop through the student_id and check if the user did not input anything in the student_id field, then insert the leader_id. But if the user did input 1 or more student_id's , then I looped trough the array and stored the inputs that had a value in them. Then I inserted the data into the database.

Teams Database (EMPTY)

team_id | leader_id | student_id

PHP Code

<?php 

error_reporting(0);
require '../../connect.php';

$leader_id = $_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'],
);

$student_save = array();

if(!empty($leader_id)) {

    foreach ($students as $student) {

        if(isset($student)) {
            array_push($student_save, $student);
        } else {
            $insert = mysqli_query($link, "INSERT INTO teams (leader_id) VALUES ('$leader_id')");

            header("Location: ../../../admin.php?message=Success!");

            break;
        }

    }

    foreach ($student_save as $student) {
        // PROBLEM
        $insert = mysqli_query($link, "INSERT INTO teams ($leader_id, $student_id) VALUES ($leader_id, $student)");

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

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

?>

If you have any question, please ask me.

  • 写回答

1条回答 默认 最新

  • douyingmou1389 2017-04-04 00:08
    关注

    The problem was this:

    $insert = mysqli_query($link, "INSERT INTO teams ($leader_id, $student_id) VALUES ($leader_id, $student)");
    

    There was a dollar sign in the INSERT

    It should've been:

    $insert = mysqli_query($link, "INSERT INTO teams (leader_id, student_id) VALUES ($leader_id, $student)");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi