将变量中的信息添加到数据库PHP中

I'm trying to add $_SESSION['user_id'] into a database and, when I echo it in this function, it works fine. However, when I try to push it into my MySQL database, it adds the value 0. Really confused as to why. Thanks for any help!

function fill_team() {
    $i = 1;
    while ($i < 24) {
        $first_name = first_name();
        $last_name = second_name();
        echo "<br>";
        $add_names = mysql_query("INSERT INTO `players` (`first_name`, `last_name`, `user_id`) VALUES ('$first_name', '$last_name', '.$_SESSION['user_id']'.)");
        $i++;
    }
}

Replace echo $row2['first_name']; with return $row2['first_name'];. If you want to get some value from the function, you should use return operator to pass the value back. It has nothing to do with printing the value with print or echo.

There's some messy mixed quoting inside the SQL statement:

mysql_query("INSERT INTO `players` (`first_name`, `last_name`, `user_id`) 
VALUES ('$first_name', '$last_name', '.$_SESSION['user_id']'.)");

Off the top of my head, I have no idea what SQL this will produce, but this should be written as:

mysql_query("INSERT INTO `players` (`first_name`, `last_name`, `user_id`) 
VALUES ('$first_name', '$last_name', '$_SESSION[user_id]')");

or

mysql_query("INSERT INTO `players` (`first_name`, `last_name`, `user_id`) 
VALUES ('$first_name', '$last_name', '".$_SESSION['user_id']."')");