我的MySQL查询和while循环和SESSIONS不能正常使用PHP

时间:2014-02-09 17:24:59

标签: php mysql session while-loop

在我的注册表单中,我会在用户提交所有数据后自动将用户登录。在将数据提交到数据库之后,我运行查询以从数据库中获取数据并将其存储在会话变量中。问题是while循环只存储第一个变量,即用户ID,而不存储其余变量。我做错了什么?

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
                            }

最后它确实回显你已登录。另外,我回显了会话变量中的值以进行测试,但所有返回值都是<br>而不是实际值,除了用户ID开始。我也没有任何错误。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

while ($row = mysqli_fetch_assoc($sql3))

您忘记添加{,并且在循环结束时似乎也是}。 因此,只有第一个命令在循环中发生并执行。

另一件事,循环后的“其他” - 它与哪种条件有关?

通常我认为缩进是好的,但你把它放得太远了。 也许,而不是嵌套太多的条件和循环,找到另一种方法。