0

我有一个 PHP 文件User.php,其中包含一个类User、一些私有全局变量、getter 和 setter、一个构造函数以及一个连接到数据库的方法。

<?php
class User {

    //Some Private Global Variables ($firstName,$lastName,etc..)
    //Getters and Setters

    public function getUserByID($userID) {
        //Create Instance
        $user = new User();
        //Get User Info
        $con=mysqli_connect("localhost","root","","mydatabase");
        if ($con->connect_error) {
            die('Connect Error: ' . $con->connect_error);
        }
        $query ='SELECT user_id,first_name,last_name,email,password,birth_date,gender,join_date '.
                'FROM users '.
                'WHERE user_id = ?';
        $stmt = $con->stmt_init();
        if($stmt->prepare($query)) {
            $stmt->bind_param('s',$userID);
            $stmt->execute();
            $stmt->bind_result($user->userID,$user->firstName,$user->lastName,$user->email,$user->password,$user->birthDate,$user->gender,$user->joinDate);
            if($stmt->fetch()==null) {
                //User doesn't exist
                return null;
            }
            $stmt->close();
            $con->close();
            return $user;
        }
    }
?>

在另一个包含表单的文件中,如果我之前执行以下操作if( isset($_POST['submit']) )

$user = new User();
$user1 = $user->getUserByID('222');

一切正常,但如果它写在它之后,我填写表格并发布它,它不会执行,我收到这个:

致命错误:第 113 行 C:\xampp\htdocs\Classes\User.php 中的最大执行时间超过 30 秒

更新

表格,很抱歉代码太长,但它正在验证中......

if (isset($_POST['submit'])){
                //Get submitted values
                $firstname = $_POST["firstname"];
                $lastname = $_POST["lastname"];
                $email = $_POST["email"];
                $password = $_POST["password"];
                $month = $_POST["month"];
                $day = $_POST["day"];
                $year = $_POST["year"];
                $gender = $_POST["gender"];
                //Validation
                //First Name
                $firstname_regex = '/^[a-zA-Z -]+$/';
                if($firstname=="" || ctype_space($firstname))
                {
                    $firstnameValid = false;
                    $firstnameError = "First Name is Required";
                }
                else if(!preg_match($firstname_regex, $firstname))
                {
                    //First Name must contain letters, spaces, dashes, and capital letter
                    $firstnameValid = false;
                    $firstnameError = "First Name contains Invalid Characters";
                }
                else
                {
                    //Valid
                    $firstnameValid = true;
                    $firstnameError = "";
                }
                //Last Name
                $lastname_regex = '/^[a-zA-Z -]+$/';
                if($lastname=="" || ctype_space($lastname))
                {
                    $lastnameValid = false;
                    $lastnameError = "Last Name is Required";
                }
                else if(!preg_match($lastname_regex, $lastname))
                {
                    //Last Name must contain letters, spaces, dashes, and capital letter
                    $lastnameValid = false;
                    $lastnameError = "Last Name contains Invalid Characters";
                }
                else
                {
                    $lastnameValid = true;
                    $lastnameError = "";
                }
                //Email
                $email_regex = '/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/';  
                if($email=="" || ctype_space($email))
                {
                    $emailValid = false;
                    $emailError = "Email is Required";
                }
                else if(!preg_match($email_regex, $email))
                {
                    $emailValid = false;
                    $emailError = "Email is not Valid";
                }
                else
                {
                    //Email is Valid, Check Database
                    $user1 = new User();
                    $user = $user1->getUserByEmail($email);
                    if($user!=null) {
                        //Email already Exists
                        $emailValid = false;
                        $emailError = "Email is already in Use";
                    }
                    else
                    {
                        $emailValid = true;
                        $emailError = "";
                    }


                }
                if($password=="" || ctype_space($password))
                {
                    $passwordValid = false;
                    $passwordError = "Password is Required";
                }
                else if(!preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $password))
                {
                    //Must be 8, contain 1 UPPER, 1 LOWER, 1 DIGIT
                    $passwordValid = false;
                    $passwordError = "Password must contain at least:<br/>1 Upper Case Letter<br/>1 Lower Case Letter<br/>1 Digit<br/>and minimum 8 Characters";
                }
                else
                {
                    //Valid
                    $passwordValid = true;
                    $passwordError = "";
                }
                if($month=="" || ctype_space($month) || $month=="Month")
                {
                    $monthValid = false;
                    $monthError = "Month is Required";
                }
                else if($month !="January" && $month !="February" && $month !="March" && $month !="April" && $month !="May" && $month !="June" && $month !="July" && $month !="August" && $month !="September" && $month !="October" && $month !="November" && $month !="December")
                {
                    //Invalid Month
                    $monthValid = false;
                    $monthError = "Month is not Valid";
                }
                else
                {
                    $monthValid = true;
                    $monthError = "";
                }
                if($day=="" || ctype_space($day) || $day=="Day")
                {
                    $dayValid = false;
                    $dayError = "Day is Required";
                }
                else if($day<1 || $day>31)
                {
                    $dayValid = false;
                    $dayError = "Day is not Valid";
                }
                else
                {
                    $dayValid = true;
                    $dayError = "";
                }
                if($year=="" || ctype_space($year) || $year=="Year")
                {
                    $yearValid = false;
                    $yearError = "Year is Required";
                }
                else if($year<1905 || $year>2013)
                {
                    $yearValid = false;
                    $yearError = "Year is not Valid";
                }
                else
                {
                    $yearValid = true;
                    $yearError = "";
                }
                if($gender=="" || ctype_space($gender) || $gender=="Are you Male or Female?")
                {
                    $genderValid = false;
                    $genderError = "Gender is Required";
                }
                else if($gender != "Male" && $gender !="Female")
                {
                    $genderValid = false;
                    $genderError = "Gender is not Valid";
                }
                else
                {
                    $genderValid = true;
                    $monthError = "";
                }
                //Check Date (leap year)
                if($monthValid==true && $dayValid==true && $yearValid==true)
                {
                    //All inputs are Valid
                    //Check 31
                    if($month == "April" && $day>30)
                    {
                        $dateValid = false;
                    }
                    else if($month == "June" && $day>30)
                    {
                        $dateValid = false;
                    }
                    else if($month == "September" && $day>30)
                    {
                        $dateValid = false;
                    }
                    else if($month == "November" && $day>30)
                    {
                        $dateValid = false;
                    }
                    else if($month == "February")
                    {
                        if($day>29)
                        {
                            $dateValid=false;
                        }
                        else
                        {
                            //Check if Year is Leap year
                            $leap = false;
                            if ($year % 4 == 0)
                            {
                                if ($year % 100 == 0)
                                {
                                    if ($year % 400 == 0)
                                    {
                                        //Leap, 29
                                        $leap = true;
                                    }
                                    else
                                    {
                                        //not leap, 28
                                        $leap = false;
                                    }
                                }
                                else
                                {
                                    //Leap, 29
                                    $leap = true;
                                }
                            }
                            else
                            {
                                //Not Leap, 28
                                $leap = false;
                            }
                            if($leap==false && $day==29)
                            {
                                $dateValid = false;
                            }
                            else
                            {
                                $dateValid = true;
                            }
                        }
                    }
                    else
                    {
                        $dateValid = true;
                    }
                }
                if($firstnameValid==true && $lastnameValid==true && $emailValid==true && $passwordValid==true && $dateValid==true && $genderValid==true)
                {
                    //Everything is Valid -> Register
                    //Fix Dates
                    if($month=="January")
                        $month=1;
                    else if($month=="February")
                        $month=2;
                    else if($month=="March")
                        $month=3;
                    else if($month=="April")
                        $month=4;
                    else if($month=="May")
                        $month=5;
                    else if($month=="June")
                        $month=6;
                    else if($month=="July")
                        $month=7;
                    else if($month=="August")
                        $month=8;
                    else if($month=="September")
                        $month=9;
                    else if($month=="October")
                        $month=10;
                    else if($month=="November")
                        $month=11;
                    else if($month=="December")
                        $month==12;
                    $birthdate = $year."-".$month."-".$day;
                    //Generate Unique ID
                    $exists = true;
                    while($exists)
                    {
                        $userID = Functions::generateID(32);
                        $user1 = new User();
                        $user = $user1->getUserByEmail($email);
                        if($user!=null) {
                            //ID is taken, Generate a new one
                            $exists = false;
                        }
                    }
                    //INSERT
                    $con=mysqli_connect("localhost","root","","mydatabase");
                    if ($con->connect_error) {
                        die('Connect Error: ' . $con->connect_error);
                    }
                    $stmt = $con->stmt_init();
                    if($stmt->prepare("INSERT INTO users (user_id,first_name,last_name,email,password,birth_date,gender,join_date) VALUES (?,?,?,?,?,?,?,NOW())"))
                    {
                        //Capital First Letter
                        $firstname = ucfirst($firstname);
                        $lastname = ucfirst($lastname);
                        $stmt->bind_param('sssssss', $userID,$firstname,$lastname,$email,$password,$birthdate,$gender);
                        $stmt->execute();
                        $stmt->close();
                        //header("Location: http://localhost/activate.php");
                        //GENERATE activation code
                        $activationCode = Functions::generateID(8);
                        //ACTIVATION CODE ready
                        $stmt1 = $con->stmt_init();
                        if($stmt1->prepare("INSERT INTO activation (user_id,activation_code) VALUES (?,?)"))
                        {
                            $stmt1->bind_param('ss', $userID,$activationCode);
                            $stmt1->execute();
                            $stmt1->close();
                            $con->close();
                            //Activation Code has been stored in Database
                            //Send Email + Redirect
                            header("Location: http://localhost/activate.php?email=".$email);

                        }
                    }
                }
            }
4

1 回答 1

0

您的while ($exists)循环代码似乎错误。你应该设置$exists = falseif $user == null,你正在做相反的事情。另外,为什么要生成一个 ID,然后再次查找具有给定电子邮件地址的用户?您不应该寻找具有相同ID的用户吗?

于 2013-03-23T23:14:43.173 回答