简体   繁体   中英

My MySQL query and while loop and SESSIONS aren't working PHP

In my signup up form I automatically log the user in after they submit all their data. After they submit their data to the database, I run a query to grab that data from the database and store it in session variables. The problem is that The while loop only stores the first variable which is the user ID and doesn't store the rest. What I am Doing wrong?

if ($signup) {
      //user clicked the register button
      if (isset($username, $em, $pass, $pass2, $fn, $ln, $sex, $bd_day, $bd_month, $bd_year)) {
         //if all of these have a value
         if ($pass == $pass2) {
              //if the passwords match
              $sql = mysqli_num_rows(mysqli_query($con, "SELECT * FROM users WHERE username = '$username'"));
              //check if username is already taken
              if ($sql < 1) {
                    //if the username hasn't been taken
                    $sql2 = mysqli_num_rows(mysqli_query($con, "SELECT * FROM users WHERE email = '$em'"));
                    //check if the email is taken
                           if ($sql2 < 1) {
                                 //the email hasn't been taken
                                 if (strlen($pass) > 5) {
                                       //password is more than 5 characters
                                       $pass = md5($pass);
                                       //md5 is not secure!!! use something else later
                                       if (strlen($username) > 4) {
                                              //username is bigger than 4 characters
                                              $query = mysqli_query($con, "INSERT INTO users (username, email, first_name, last_name, sex, bd_month, bd_year, bd_day, password, profile_pic, signup_date, activated, cover_photo) VALUES ('$username','$em','$fn', '$ln', '$sex', '$bd_month', '$bd_year', '$bd_day', '$pass', 'profilepic/default.png','$date)','0', 'coverphoto/defaultcover.jpg')") or die(mysqli_error($con));
                                               //insert into db
                                               $sql3 = mysqli_query($con, "SELECT * FROM users WHERE username = '$username' AND email = '$em'") or die(mysqli_error($con));
                                               //select values from db for login
                                               $num = mysqli_num_rows($sql3);
                                               //check how many hits the query gets it should be 1
                                               if ($sql3 == true && $num > 0) {
                                                     //check if query was a success and that there is a user with those credentials
                                                     while ($row = mysqli_fetch_assoc($sql3))
                                                                //log user in
                                                            $_SESSION['id'] = $row['id'];
                                                            echo $_SESSION['id'] . '<br>';
                                                            $_SESSION['un'] = $row['username'];
                                                            echo $_SESSION['un']. '<br>';
                                                            $_SESSION['fn'] = $row['first_name'];
                                                            echo $_SESSION['fn'] . '<br>';
                                                            $_SESSION['ln'] = $row['last_name'];
                                                            echo $_SESSION['ln'] . '<br>';
                                                            $_SESSION['em'] = $row['email'];
                                                            echo $_SESSION['em'] . '<br>';
                                                            $_SESSION['pp'] = $row['profile_pic'];
                                                            echo $_SESSION['pp'] . '<br>';
                                                            $_SESSION['signup_date'] = $row['signup_date'];
                                                            $_SESSION['active'] = $row['activated'];
                                                            setcookie('un', $username, time()+3600*60*24*7*4);
                                                            echo "You have been logged in.";
                                                            exit();
                                                        } else {
                                                            echo "An unknown error occurred";
                                                            exit();
                                                        }
                                                    } else {
                                                        //username is less than 4 characters
                                                        echo "Your username must be larger than 4 characters!";
                                                        exit();
                                                    }
                                                } else {
                                                    //password is less than 5 characters
                                                    echo "You password must be more than 5 characters long<br />";
                                                    exit();
                                                }
                                            } else {
                                                //email is already taken
                                                echo "That email has already been taken!";
                                                exit();
                                            }
                                        } else {
                                            //username is already taken
                                            echo "That username is already taken!";
                                            exit();
                                        }
                                    } else {
                                        //the passwords don't match
                                        echo "You passwords don't match";
                                        exit();
                                    }
                                } else {
                                    //user didn't submit the form
                                    echo "Please fill in all the fields";
                                    exit();
                                }
                            } else {
                                //user didn't click the register button
                            }

In the end it does echo You have been logged in. Also, I echoed out the values in the session variables for testing but all the return are the <br> not the actual values except for the user ID at the beginning. I don't get any errors either.

Please help me.

while ($row = mysqli_fetch_assoc($sql3))

You forgot to add { and it seems that also } at the end of the loop. So only the first command takes place in the loop and executed.

Another thing, the "else" after the loop - to which condition is it relate?

Usually I think indents are good, but you took it too far. Maybe, instead of nesting too many conditions and loops, find another approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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