程序输出错误的字符串

时间:2017-03-28 23:32:29

标签: php mysql sql

我有一个程序,用户输入了司机的名字,姓氏,他们的团队领导者的身份证,电话号码和区号。然后它将信息存储到数据库中,但不是存储领导者的ID,而是找到与领导者ID相对应的团队ID并存储它。

示例

驱动程序表

driver_id | first_name | last_name | phone | area_code | team_id

团队表

team_id | leader_id | student_id

因此程序会找到与用户输入的team_id对应的leader_id

问题

我的问题是如果leader_id不存在,这意味着团队不存在,所以程序应该输出这行代码header("Location: ../../../admin.php?message=Team Doesn't Exist");(因为table为空)但是程序会输出这行代码header("Location: ../../../admin.php?message=Success!");,如果leader_id存在则应该输出。

PHP代码

<?php

require '../../connect.php';

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$leader_id = $_POST['leader_id'];
$phone = $_POST['phone'];
$area_code = $_POST['area_code'];

if(!empty($first_name) && !empty($last_name) && !empty($phone) && !empty($leader_id)) {

    if(is_numeric($leader_id) && is_numeric($phone) && is_numeric($area_code)) {

        $get_team_id = mysqli_query($link, "SELECT team_id FROM teams WHERE leader_id = $leader_id");

        if($get_team_id) {

            $result = mysqli_query($link, "INSERT INTO drivers (first_name, last_name, phone, area_code, team_id) SELECT '$first_name', '$last_name', '$phone', '$area_code', team_id FROM teams WHERE leader_id = '$leader_id'");

            if($result) {

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

            } else {

                header("Location: ../../../admin.php?message=Sorry we ran into an error");

            }

            header("Location: ../../../admin.php?message=Team Exists");

        } else {

            header("Location: ../../../admin.php?message=Team Doesn't Exist");

        }


    } else {

        header("Location: ../../../admin.php?message=Please add a number for the Leader ID, Phone Number and Area Code");

    }
} else if (empty($first_name) || empty($last_name) || empty($leader_id) || empty($phone)) {

    header("Location: ../../../admin.php?message=Please add you're input values");

}

&GT;

1 个答案:

答案 0 :(得分:0)

尝试下面的条件代码
if(mysqli_num_rows($get_team_id)>0)
而不是
if($get_team_id)