drl6054 2014-02-09 17:24 采纳率: 0%
浏览 39
已采纳

我的MySQL查询和while循环和SESSIONS不能正常使用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.

  • 写回答

1条回答 默认 最新

  • duandan4680 2014-02-09 17:32
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗