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.

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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面