#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Php register script , getting error


    Hi ,
    I am new to PHP scripting and new to the forum as well.

    I learnt a few concepts of PHP and was trying to create a registration form . I can do this easily , using a simple piece of code , but I intend to do it with OOP way.

    I have tried to put some pieces of code altogether ( I wrote them myself.), but apparently they didn't work.

    I was wondering if someone can help me out with my mistakes in the code.

    Thanks.

    Here is the code :
    PHP Code:
    <?php


    class register

    {


    $fname;

    $lname;

    $uname;

    $email;

    $pass;

    $reg_date;

    $execute=false;



    try

    {

    $pdo_object=new PDO("mysql:host=localhost;dbname=*******""********""********");
    }

    catch(
    PDOException $pd)

    {

    echo
    'Error occured while connecting to the database.'.$pd->getMessage();
    }


    public 
    _construct($fname,$lname,$uname,$email,$pass,$reg_date)


    {

    $this->fname=$fname;

    $this->lname=$lname;

    $this->uname=$uname;

    $this->email=$email;

    $this->pass=$pass;

    $this->reg_data=$reg_date;

    }



    public function 
    insert_user()

    {
    try

    {

    $reg_query='INSERT into users SET firstname=:firstname,lastname=:lastname,username=:username,email=:email,password=:password,join_date=:join_date';

    $process=$pdo_object->prepare($reg_query);

    $process=bindValue(':firstname',$this->fname,':lastname',$this->lname,':username',$this->uname,':email',$this->email,':password',$this->pass,':join_date',$this->reg_date);

    $process=execute();

    }

    catch(
    PDOException $exc)

    {

    echo
    'Error Occured While processing Registration.'.$exc->getMessage();

    exit();

    }

    }



    public function 
    checklist()

    {


    try

    {

    $query='SELECT username FROM users WHERE username='".$this->uname";

    $process1=$pdo_object->query($query);


    while(!
    $process1->fetch())

    {

    $this->execute=true;


    if(empty(
    $this->fname))

    {

    echo
    'Firstname is Empty!';

    $this->execute=false;

    }

    if(empty(
    $this->lname))

    {

    echo
    'Lastname is Empty!';

    $this->execute=false;

    }

    if(empty(
    $this->uname))

    {

    echo
    'Username is empty!';

    $this->execute=false;

    }

    if(empty(
    $this->email))

    {

    echo
    'Email is empty!';

    $this->execute=false;

    }

    if(empty(
    $this->pass))

    {
    echo
    'Password is Empty!';

    $this->execute=false;

    }

    if(
    $this->pass!=$_REQUEST['password_confirm])

    {

    echo'
    Passwords do not match.!';

    $this->execute=false;

    }

    }


    }

    catch(PDOException $exc)

    {

    echo'
    Failed to Check User.'.$exc->getMessage();

    }

    }


    }



    while(isset($_REQUEST['
    register']))

    {



    $object=new register($_REQUEST['
    firstname'],$_REQUEST['lastname'],$_REQUEST['username'],$_REQUEST['email'],$_REQUEST['password'],$_REQUEST['reg_date']);


    $object->checklist();

    if($object->execute==true)

    {

    $object->insert_user();

    }

    else

    {

    echo'
    Failed!';

    }

    }



    exit();

    ?>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    OK, where do I start...

    First of all: What error do you get? A syntax error? A runtime error? A blank screen? Unexpected behaviour? Wrong output? We're not psychic. So if people just dump a script and tell us "it doesn't work", that makes it very difficult to help.

    From what I can tell, this is pretty far from valid PHP code. There are plenty of syntax errors and things that simply don't work in PHP (and probably don't even make sense). The main problem is that you've never actually tested your code while you wrote it. Programming blind may work if you're extremely talented and experienced, but it doesn't work if you've just started programming. So don't do it.

    Write your code step by step. Start with the basic structure, leaving out all the actual functionalities. Then test it. If it works, move on to the next step. If it doesn't, debug your code. And so on. This way you only have to deal with small pieces of code. If something goes wrong, you can locate the issue pretty easily, because you know where to look. Whereas if you write down 200 lines of untested code, you're likely to end up with a total mess you can no longer debug yourself.

    You also need to start formatting your code with proper indentation. This is not about making the code look pretty, it's about making it readable. Nobody can read 100 curly braces all crammed into one column, and that's when people start to make syntax errors.

    So much for the theory. In your concrete case, you should create a blank file, put your current code next to it and then rewrite the code. Start with the class statement:

    PHP Code:
    <?php

    class register
    {

        
    $fname;
        
    $lname;
        
    $uname;
        
    $email;
        
    $pass;
        
    $reg_date;
        
    $execute=false;

    }
    Does PHP accept this? No, it already chokes on this small piece of code, giving you a syntax error:

    Code:
     Parse error: syntax error, unexpected '$fname' (T_VARIABLE), expecting function (T_FUNCTION) in ...
    So obviously there's something wrong with the $fname. The problem is that you cannot put bare variables into a class body. PHP only recognizes them as attributes if there's a modifer in front of them (public, private, static, ...). In your case, decide whether you want private or public attributes and then put the corresponding modifier in front of the variables:

    PHP Code:
    <?php

    class register
    {

        private 
    $fname;
        private 
    $lname;
        private 
    $uname;
        private 
    $email;
        private 
    $pass;
        private 
    $reg_date;
        private 
    $execute false;

    }
    Run the code again. Does it work? It should.

    The next issue is that you cannot put arbitrary code into a class body. You cannot have a try-catch statement or anything like that within a class declaration. All application logic has to go into the methods of the class. In your case, the PDO initialization belongs into the constructor. Do that and test your code again.

    By the way, kudos to you for being one of the very few beginners to use PDO and prepared statements right from the start! This is very, very rare. Many PHP people don't give a fck about security or even code quality.
    Last edited by Jacques1; June 19th, 2013 at 06:56 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    OK, where do I start...

    First of all: What error do you get? A syntax error? A runtime error? A blank screen? Unexpected behaviour? Wrong output? We're not psychic. So if people just dump a script and tell us "it doesn't work", that makes it very difficult to help.

    From what I can tell, this is pretty far from valid PHP code. There are plenty of syntax errors and things that simply don't work in PHP (and probably don't even make sense). The main problem is that you've never actually tested your code while you wrote it. Programming blind may work if you're extremely talented and experienced, but it doesn't work if you've just started programming. So don't do it.

    Write your code step by step. Start with the basic structure, leaving out all the actual functionalities. Then test it. If it works, move on to the next step. If it doesn't, debug your code. And so on. This way you only have to deal with small pieces of code. If something goes wrong, you can locate the issue pretty easily, because you know where to look. Whereas if you write down 200 lines of untested code, you're likely to end up with a total mess you can no longer debug yourself.

    You also need to start formatting your code with proper indentation. This is not about making the code look pretty, it's about making it readable. Nobody can read 100 curly braces all crammed into one column, and that's when people start to make syntax errors.

    So much for the theory. In your concrete case, you should create a blank file, put your current code next to it and then rewrite the code. Start with the class statement:

    PHP Code:
    <?php

    class register
    {

        
    $fname;
        
    $lname;
        
    $uname;
        
    $email;
        
    $pass;
        
    $reg_date;
        
    $execute=false;

    }
    Does PHP accept this? No, it already chokes on this small piece of code, giving you a syntax error:

    Code:
     Parse error: syntax error, unexpected '$fname' (T_VARIABLE), expecting function (T_FUNCTION) in ...
    So obviously there's something wrong with the $fname. The problem is that you cannot put bare variables into a class body. PHP only recognizes them as attributes if there's a modifer in front of them (public, private, static, ...). In your case, decide whether you want private or public attributes and then put the corresponding modifier in front of the variables:

    PHP Code:
    <?php

    class register
    {

        private 
    $fname;
        private 
    $lname;
        private 
    $uname;
        private 
    $email;
        private 
    $pass;
        private 
    $reg_date;
        private 
    $execute false;

    }
    Run the code again. Does it work? It should.

    The next issue is that you cannot put arbitrary code into a class body. You cannot have a try-catch statement or anything like that within a class declaration. All application logic has to go into the methods of the class. In your case, the PDO initialization belongs into the constructor. Do that and test your code again.

    By the way, kudos to you for being one of the very few beginners to use PDO and prepared statements right from the start! This is very, very rare. Many PHP people don't give a fck about security or even code quality.


    Thanks for your help. I will try to follow the rules of programming you have mentioned. And I apologize for not mentioning the error code.
    I didn't had access to any compiler or IDE , so I couldn't test the code in bits/modules.I wrote it in notepad and ran it on the remote web server.It just gave me a internal server error!. Sorry about that.

    Actually I just started with PHP few days ago , so I missed on some important concepts. I will re-read them.

    I will try to download an IDE , and implement the code on it module by module.

    Thanks again for your assistance. I found it very helpful.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by rishi.
    I wrote it in notepad and ran it on the remote web server.
    That's not a good idea. Broken PHP code can compromise the whole server. I'm not only taking about "stupid" mistakes. Anybody on this planet will be able to see your unfinished website and use any security hole to attack you.

    So don't use a live server for experimenting. Set up a local test enviroment with XAMPP (or one its variants).



    Originally Posted by rishi.
    I will try to download an IDE , and implement the code on it module by module.
    An IDE is no absolute necessity. Personally, I've used plain editors for a long time, because I found IDEs too bloated.

    Sure, they can be very helpful. But it's not like you needed them for writing good code. Everything I described could be done in plain Notepad.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by Jacques1
    Sure, they can be very helpful. But it's not like you needed them for writing good code. Everything I described could be done in plain Notepad.
    I've always been a text editor guy, myself, but then I've been programming since I was 8. For someone just starting out I can see where at least the syntax highlighting offered by IDEs (or, granted, some text editors) might help those who are still overcoming simple syntax issues.

    Not sure what I'd recommend, though. UltraEdit seems decent, which I sort of have to use at my new job, but I think it's licensed.
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    I don't use an IDE either, but Notepad is terrible, nearly anything would be better than it.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Edited.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    Try notepad++ it's free with many features and has syntax highlighting while being as easy to use as notepad is

    Comments on this post

    • badger_fruit agrees : Yeah, Notepad++ is awesome, it has some good features like code highlighting too, not bad for a light free package!

IMN logo majestic logo threadwatch logo seochat tools logo