SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Class problems

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class problems

    Hey everyone,

    I'm just getting into creating my own classes and my user registration class has gone wrong somewhere. My errors aren't appearing. If you notice any other errors please let me know.

    Code PHP:
    <?php
    class user{
        public $submit = true;
        public $emailErr;
        public $passwordErr;
        public $firstErr;
        public $lastErr;
        public $dbUser = false;
        public $dbEmail = false;
        function wrapErr($err){
            return "<div class=\"error\">".$err."</div>";
        }
        function validateReg(){
            if(!preg_match("/[a-zA-Z0-9]{6,15}/", $_POST['password'])){
                $submit = false;
                $passwordErr = user::wrapErr("Your password must be between 6 and 15 characters and cannot contain special characters.");
            } else if($_POST['password'] != $_POST['confPassword']){
                $submit = false;
                $passwordErr = user::wrapErr("Your passwords do not match");
            }
            if(preg_match("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$_POST['email'])){
                $qry = mysql_query("SELECT email FROM users WHERE email='$email'");
                if(mysql_num_rows($qry) > 0){
                    $submit = false;
                    $emailErr = user::wrapErr("The email you entered is already registered.");
                }
            } else {
                $submit = false;
                $emailErr = user::wrapErr("Please enter a valid email");
            }
            if(!preg_match('/[a-zA-Z]{1,}/', $_POST['first'])){
                $firstErr = user::wrapErr("Please enter a first name containing only letters.");
            }                        
            if(!preg_match('/[a-zA-Z]{1,}/', $_POST['first'])){
                $lastErr = user::wrapErr("Please enter a last name containing only letters.");
            }
            if($submit){
                return true;
            } else {
                return false;
            }     
        }
        function buildReg(){
            if(isset($_POST['submit'])){
                if(user::validateReg()){
                    return user::insertReg();
                } else {
                    $form = $firstErr. $lastErr.$emailErr.$passwordErr;     
                }
            }
            $form .= "<form name=\"regForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">First Name:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['first']."\" name=\"first\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Last Name:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['last']."\" name=\"first\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Email:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['email']."\" name=\"first\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Password:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['password']."\" name=\"first\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Confirm Password:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['confPassword']."\" name=\"first\"></span>";
            $form .= "</div>";
            $form .= "<div><input type=\"submit\" name=\"submit\" value=\"Register\"></div>";                      
            $form .= "</form>";
            return $form;
        }
        function insertReg(){
            mysql_query("INSERT INTO users (first, last, email, password) VALUES (".$_POST['first'].",".$_POST['last'].",".$_POST['email'].",".$_POST['password'].")");
            header('Location: newReg.php');
        }
    }  
    ?>

    Thanks,
    e39m5

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure what exactly your problem. What it should work and what is doing now. When tested in my system then it displays the form but since i do not have table, i did not test with insertion.

    Could you be more descriptive about problem please?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    The validation errors aren't appearing when I test it. I noticed that my "name" attributes were not correct, but still no errors. It doesn't even seem like validateReg(); is being called.

    You can // out the insert line, the problem isn't there, yet. I think once I get the validation working I'll be able to figure out why my insert wouldn't work.

    Code updated with proper name attributes below:
    Code PHP:
    <?php
    class user{
        public $submit = true;
        public $emailErr;
        public $passwordErr;
        public $firstErr;
        public $lastErr;
        public $dbUser = false;
        public $dbEmail = false;
        function wrapErr($err){
            return "<div class=\"error\">".$err."</div>";
        }
        function validateReg(){
            if(!preg_match("/[a-zA-Z0-9]{6,15}/", $_POST['password'])){
                $submit = false;
                $passwordErr = user::wrapErr("Your password must be between 6 and 15 characters and cannot contain special characters.");
            } else if($_POST['password'] != $_POST['confPassword']){
                $submit = false;
                $passwordErr = user::wrapErr("Your passwords do not match");
            }
            if(preg_match("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$_POST['email'])){
                $qry = mysql_query("SELECT email FROM users WHERE email='$email'");
                if(mysql_num_rows($qry) > 0){
                    $submit = false;
                    $emailErr = user::wrapErr("The email you entered is already registered.");
                }
            } else {
                $submit = false;
                $emailErr = user::wrapErr("Please enter a valid email");
            }
            if(!preg_match('/[a-zA-Z]{1,}/', $_POST['first'])){
                $firstErr = user::wrapErr("Please enter a first name containing only letters.");
            }                        
            if(!preg_match('/[a-zA-Z]{1,}/', $_POST['first'])){
                $lastErr = user::wrapErr("Please enter a last name containing only letters.");
            }
            if($submit){
                return true;
            } else {
                return false;
            }     
        }
        function buildReg(){
            if(isset($_POST['submit'])){
                if(user::validateReg()){
                    return user::insertReg();
                } else {
                    $form = $firstErr. $lastErr.$emailErr.$passwordErr;     
                }
            }
            $form .= "<form name=\"regForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">First Name:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['first']."\" name=\"first\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Last Name:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['last']."\" name=\"last\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Email:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['email']."\" name=\"email\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Password:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['password']."\" name=\"password\"></span>";
            $form .= "</div>";
            $form .= "<div class=\"regForm\">";
            $form .= "<span class=\"regFormLeft\">Confirm Password:</span>";
            $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['confPassword']."\" name=\"confPassword\"></span>";
            $form .= "</div>";
            $form .= "<div><input type=\"submit\" name=\"submit\" value=\"Register\"></div>";                      
            $form .= "</form>";
            return $form;
        }
        function insertReg(){
            mysql_query("INSERT INTO users (first, last, email, password) VALUES (".$_POST['first'].",".$_POST['last'].",".$_POST['email'].",".$_POST['password'].")");
            header('Location: newReg.php');
        }
    }  
    ?>

    Thanks,
    e39m5

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See this is working and i think you can find what you had missed:
    PHP Code:
    class user{
        public 
    $submit true;
        public 
    $emailErr;
        public 
    $passwordErr;
        public 
    $firstErr;
        public 
    $lastErr;
        public 
    $dbUser false;
        public 
    $dbEmail false;
        function 
    wrapErr($err){
            return 
    "<div class=\"error\">".$err."</div>";
        }
        function 
    validateReg(){
            if(!
    preg_match("/[a-zA-Z0-9]{6,15}/"$_POST['password'])){
                
    $submit false;
                
    $this->passwordErr $this->wrapErr("Your password must be between 6 and 15 characters and cannot contain special characters.");
            } else if(
    $_POST['password'] != $_POST['confPassword']){
                
    $submit false;
                
    $this->passwordErr $this->wrapErr("Your passwords do not match");
            }
            if(
    preg_match("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$_POST['email'])){
                
    $qry mysql_query("SELECT email FROM users WHERE email='$email'");
                if(
    mysql_num_rows($qry) > 0){
                    
    $submit false;
                    
    $this->emailErr $this->wrapErr("The email you entered is already registered.");
                }
            } else {
                
    $submit false;
                
    $this->emailErr $this->wrapErr("Please enter a valid email");
            }
            if(!
    preg_match('/[a-zA-Z]{1,}/'$_POST['first'])){
                
    $this->firstErr $this->wrapErr("Please enter a first name containing only letters.");
            }                       
            if(!
    preg_match('/[a-zA-Z]{1,}/'$_POST['first'])){
                
    $this->lastErr $this->wrapErr("Please enter a last name containing only letters.");
            }
            if(
    $submit){
                return 
    true;
            } else {
                return 
    false;
            }     
        }
        function 
    buildReg(){
            if(isset(
    $_POST['submit'])){
                if(
    $this->validateReg()){
                    return 
    user::insertReg();
                } else {
                    
    $form $this->firstErr$this->lastErr.$this->emailErr.$this->passwordErr;     
                }
            }
            
    $form .= "<form name=\"regForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
            
    $form .= "<div class=\"regForm\">";
            
    $form .= "<span class=\"regFormLeft\">First Name:</span>";
            
    $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['first']."\" name=\"first\"></span>";
            
    $form .= "</div>";
            
    $form .= "<div class=\"regForm\">";
            
    $form .= "<span class=\"regFormLeft\">Last Name:</span>";
            
    $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['last']."\" name=\"first\"></span>";
            
    $form .= "</div>";
            
    $form .= "<div class=\"regForm\">";
            
    $form .= "<span class=\"regFormLeft\">Email:</span>";
            
    $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['email']."\" name=\"first\"></span>";
            
    $form .= "</div>";
            
    $form .= "<div class=\"regForm\">";
            
    $form .= "<span class=\"regFormLeft\">Password:</span>";
            
    $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['password']."\" name=\"first\"></span>";
            
    $form .= "</div>";
            
    $form .= "<div class=\"regForm\">";
            
    $form .= "<span class=\"regFormLeft\">Confirm Password:</span>";
            
    $form .= "<span class=\"regFormRight\"><input type=\"text\" value=\"".$_POST['confPassword']."\" name=\"first\"></span>";
            
    $form .= "</div>";
            
    $form .= "<div><input type=\"submit\" name=\"submit\" value=\"Register\"></div>";                     
            
    $form .= "</form>";
            return 
    $form;
        }
        function 
    insertReg(){
            
    mysql_query("INSERT INTO users (first, last, email, password) VALUES (".$_POST['first'].",".$_POST['last'].",".$_POST['email'].",".$_POST['password'].")");
            
    header('Location: newReg.php');
        }
    }
    $usr = new user;
    echo 
    $usr->buildReg(); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh thanks a bunch. That's one of those mistakes I'll never make again =D

    Are there any noticeable issues with the class structure itself? It works so thats a plus.

    Thanks again,
    e39m5

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if you do want your class critiqued...

    It's basically procedural, wrapped in a class statement.

    Firstly, all of your properties are public. That's not encapsolation in any sense of the term. Classes should hide their internals.

    Secondly, you're calling your methods statically. You may as well just get rid of the class and use procedural coding if they will all be static, IMO.

    You need to decide exactly what your user class will do, and have it do that one thing very, very well.

    Right now it is doing many things. It is validating the form (business logic), it is outputting the form (presentation), and it is entering the data (business logic / data access). You're crossing many logical layers in one class.

    What you need is a template for outputting the form, a class that does the business logic such as validation etc, or even perhaps several classes for data validation, and the business logic class should delegate to yet another to manipulate the data in the database.

    I hope this helps.

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I went with what you said and completely changed up what I had. Now this is my code. My main concern is that I'm building the form and errors into a public variable within the class. Should I instead "return" the form and build it that way?

    Any other suggestions on this new piece of code?
    Note: my classes are in includes, I just put it all in one chunk to show it.

    Code PHP:
    class buildForm{
        public $form;
        function openForm($action,$name,$method){
            $this->form = "<form action=\"".$action."\" name=\"".$name."\" method=\"".$method."\">";    
        }    
        function inputText($label,$type,$name,$class){
            $this->form .= "<div class=\"".$class."\">";
            $this->form .= "<span class=\"".$class."Left\">".$label.":</span>";
            $this->form .= "<span class=\"".$class."Right\"><input type=\"".$type."\" value=\"".$_POST[$name]."\" name=\"".$name."\"></span>";
            $this->form .= "</div>";
        }
        function button($value,$name,$class){
            $this->form .= "<div class=\"".$class."Span\"><input type=\"submit\" value=\"".$value."\" name=\"".$name."\"></div>";   
        }
        function closeForm(){
            $this->form .= "</form>";        
        }
    }
    class formValidate{
        public $submit = true;
        public $errors;
        function wrapErr($err){
            return "<div class=\"error\">".$err."</div>";
        }
        function pattern($pattern,$string,$error){
            if(!preg_match($pattern,$string)){
                $this->submit = false;
                $this->errors .= $this->wrapErr($error);    
            }
        }
        function match($valOne,$valTwo,$error){
            if($valOne != $valTwo){
                $this->submit = false;
                $this->errors .= $this->wrapErr($error);            
            }
        }
        function blank($value,$error){
            if(empty($value)){
                $this->submit = false;
                $this->errors .= $this->wrapErr($error);            
            }
        }
        function dbCheck($table,$column,$value,$error){
            $check = mysql_query("SELECT ".$column." FROM ".$table." WHERE ".$column."='".$value."'");        
            if(mysql_num_rows($check) > 0){
                $this->submit = false;
                $this->errors .= $this->wrapErr($error);        
            }    
        }
        function doValidate(){
            if($this->submit){
                return true;    
            } else {
                return $this->errors;    
            }
        }        
    }
    if(isset($_POST['submit'])){
        $validate = new formValidate();
        $validate->pattern("/[a-zA-Z]{1,}/",$_POST['first'],"Please enter a first name containing only letters.");    
        $validate->pattern("/[a-zA-Z]{1,}/",$_POST['last'],"Please enter a last name containing only letters."); 
        $validate->pattern("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$_POST['email'],"The email you entered is not valid.");
        $validate->dbCheck("users","email",$_POST['email'],"The email you entered already exists in our system.");
        $validate->pattern("/[a-zA-Z0-9]{6,15}/",$_POST['password'],"Your password must be between 6 and 15 characters and cannot contain special characters.");
        $validate->match($_POST['password'],$_POST['confPassword'],"Your passwords do not match.");
        $form = $validate->doValidate();
        if($valid == true){
            //Do Insert
            echo "do insert";    
        }
    }
    $buildForm = new buildForm();
    $buildForm->openForm($_SERVER['PHP_SELF'],"registration","POST");
    $buildForm->inputText("First Name","text","first","regForm");
    $buildForm->inputText("Last Name","text","last","regForm");
    $buildForm->inputText("Email","text","email","regForm");
    $buildForm->inputText("Password","password","password","regForm");
    $buildForm->inputText("Confirm Password","password","confPassword","regForm");
    $buildForm->button("Submit","submit","regForm");
    $buildForm->closeForm();
    $form .= $buildForm->form;
    echo $form;

    Thanks everyone,
    e39m5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •