简体   繁体   English

当变量的顺序不同时,PHP函数无法与数组一起正常运行

[英]PHP Function not functioning correctly with array when variables are in a different order

I have a PHP function. 我有一个PHP函数。 The function checks names and, if they do not exist, dies. 该功能检查名称,如果不存在,则死亡。 It strips out spaces from the names, and adds the name of the current user logged in if they are not already in the list. 它从名称中去除空格,并添加当前登录用户的名称(如果尚未在列表中)。

function create_group($name, $description, $invites){
    global $link;
    $invitesarr = explode(',', $invites);
    $user_id = $_SESSION['user_id']; //avoids issues with quotations (either invalid quotation for referring to PHP variable or repeated double quotes)
    $result = mysqli_query($link, "SELECT `username` FROM `users` WHERE `user_id` = '$user_id'");
    foreach($result as $resul){
        foreach($resul as $resu){
            $logged_in_username = $resu;
        }}
    if(in_array($logged_in_username, $invitesarr)){
    }else{
        $invitesarr[] = $logged_in_username;
    }

    foreach($invitesarr as $invite) {
        $idres = mysqli_query($link, "SELECT `user_id` FROM `users` WHERE `username` = '$invite'");
        $invite = preg_replace('/\s+/', '', $invite);
        print $invite;
        if(mysqli_num_rows($idres) == 0) {
            exit("1 or more of the users that you entered do(es) not exist!");
        }
    }

    $name = mysqli_real_escape_string($link, $name);
    $description = mysqli_real_escape_string($link, $description);
    $names = mysqli_query($link, "SELECT `group_name` FROM `groups` WHERE `group_name` = '$name'");
    $descriptions = mysqli_query($link, "SELECT `group_description` FROM `groups` WHERE `group_description` = '$description'");
    if(mysqli_num_rows($names) == 0 && mysqli_num_rows($descriptions) == 0) {
        mysqli_query($link, "INSERT INTO `groups` (`group_name`, `group_description`) VALUES ('$name', '$description')");
    } else {
        echo 'Group with that name/description already exists.';
    }

    $result = mysqli_query($link, "SELECT `group_id` FROM `groups` WHERE `group_name` = '$name'");

    foreach($result as $resul) {
        foreach($resul as $resu) {
            $group_id = $resu;
        }
    }
    foreach($invitesarr as $invite) {
        $idres = mysqli_query($link, "SELECT `user_id` FROM `users` WHERE `username` = '$invite'");
        foreach($idres as $idarr) {
            foreach($idarr as $id) {
                mysqli_query($link, "INSERT INTO `group_members` (`group_id`, `user_id`, `confirmed?`) VALUES ('$group_id', '$id', 0)");
            }
        }
    }
}

It works perfectly, except for when a user enters their name and it goes into the $invitesarr array. 除了用户输入其姓名并将其放入$ invitesarr数组外,它可以完美地工作。 When I put a print statement in the foreach statement that checks names, I notice that, when the user's name is not entered into the field (which posts) the names are in the order name1 loggedinname, whereas when I enter the user's name it becomes loggedinname name1. 当我在用于检查名称的foreach语句中放置一个打印语句时,我注意到,当未在该字段中输入用户名(该字段过时)时,名称按name1登录名的顺序排列,而当我输入用户名时,它变为登录名名称1。 This seems to be significant. 这似乎很重要。

You need to put: 您需要输入:

$invite = preg_replace('/\s+/', '', $invite);

before the line: 行:

$idres = mysqli_query($link, "SELECT `user_id` FROM `users` WHERE `username` = '$invite'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM