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 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流