SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: New to PDO and need some help

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    New to PDO and need some help

    Hey There,

    I am looking into PDO (to be honest never knew what it was until a week or so ago) But when i try to insert data i have it so it should echo a successfully saved message. But all i am recving is a black white page.

    Here's the class that is housing my add member.

    PHP Code:
    class addMember 
    {

        public function 
    __constuct($nameFirst,$nameLast,$email,$pass)
            {
                
    $this->email $email;
                
    $this->nameFirst $nameFirst;
                
    $this->nameLast $nameLast;
                
    $this->pass =  $pass;
                
    $this->group '2';

                
    $hashpasswd hash('sha256'$pass);
                
    $salt '';
                
    $level1paswd $hashpasswd."".$salt;
                
    $pepper '';
                
    $passWd $level1paswd."".$pepper;

                
    $queryAdd 'INSERT INTO members (nameFirst,nameLast,password,email,group) VALUES (:Firstname,:Lastname,:password,:email,:group)';

                
    $prepare $dbCon->prepare($queryAdd);
                
    $prepare->execute(array(
                    
    'Firstname' => $This->nameFirst,
                    
    'Lastname' => $this->nameLast,
                    
    'password' => $passWd,
                    
    'email' => $this->email,
                    
    'group' => $this->group)
                );

                echo 
    "<p class='success'> The member sucessfully inserted in the database they can now login in with the password you entred!</p>";
            } 
    And then on another page i am doing:

    PHP Code:

    require_once('./lib/member.php');

    $nameFirst $_POST['firstname'];
    $nameLast $_POST['lastname'];
    $email $_POST['email'];
    $pass $_POST['pass'];

    $addMember = new addMember($nameFirst,$nameLast,$email,$pass); 
    i have the Database and all that defined up in another file and am including that at the top.

    Any one seeing anything i am missing?

    Like i said i am new to PDO and not sure i fully 'get it' yet

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You missed the 'r' in __construct() , it happens to everyone. Also, I don't know if you have $dbConn as a global but there is no instance of PDO assigned to it within the class itself

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JeremyC View Post
    You missed the 'r' in __construct() , it happens to everyone. Also, I don't know if you have $dbConn as a global but there is no instance of PDO assigned to it within the class itself
    ohh thanks!! i have a spelling difficulty so glad you found that . I have $dbConn in an external include at the start of the file

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Right now i am just changing my database connection to more of an OOP meathod and i have run into a bit of an issue here's what i have

    PHP Code:
    class database {

        public function 
    __construct($host,$dbname,$user,$pass)
            {
                try {
                        
    $con = new PDO('mysql:host='.$host.';dbname='.$dbname.','.$user.',''.$pass.');
                    }

                catch (
    PDOException $e)
                    {
                        echo 
    '<p class="error">Could not connect to database</p>';
                    }
            }


    but this is where i have the worry
    PHP Code:
    ''.$pass.') 
    as you can see i have the ''.$pass.' and to me that does not make sense but if i only have
    PHP Code:
     '.$pass.'
    i am not sure if it would work or not because the syntax does not colour correctly. Maybe i am just second guessing my self (would not be the first time)

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are way too many quotes in there. $user and $pass are arguments to the PDO constructor, not a part of the connection string.

    PHP Code:
    $con = new PDO('mysql:host='.$host.';dbname='.$dbname$user$pass ); 

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    explains why i thought it was wrong. Thanks i am exitcied to work with PDO as it seems a lot easier to work with then regular old mysql that i know off heart.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm glad I could help

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    getting tired of me yet lol?

    Have another question for anyone. After I use a form to input text it seems to be passed 'successfully' and the success message is echoed but a nothing is inserted in to the database but no error is displayed saying that there was an error connecting to the database

    PHP Code:
    class addMember 
    {

        public function 
    __construct($nameFirst,$nameLast,$email,$pass)
            {
                
    $host 'localhost';
                
    $dbname 'berryclient';
                
    $user 'root';
                
    $pass '';

                try {
                        
    $con = new PDO('mysql:host='.$host.';dbname='.$dbname$user$pass );  
                    }

                catch (
    PDOException $e)
                    {
                        echo 
    '<p class="error">Could not connect to database</p>';
                    }

                
    $this->email $email;
                
    $this->nameFirst $nameFirst;
                
    $this->nameLast $nameLast;
                
    $this->pass =  $pass;
                
    $this->group '2';

                
    $hashpasswd hash('sha256'$pass);
                
    $salt '3a1fda65f1d2f3af5d4fd8f44a64erz64zcx1s3dfggsf1dgfs1G5fsdf4gfs5gFSGSgF574stzxCXCx2zx1zcv1ZCXV22VCXV5FD6S45SGF6D4654SD5GF4DS4VF3ZC2VZXCVV3CXVB5VXCV4BFDX6D4Dkshdglhsdfkjghfsjhsgfgsg1sdf32gs5gf4sg432s3g1fsd2g3sdgfd';
                
    $level1paswd $hashpasswd."".$salt;
                
    $pepper 'dksal2DFfdSa31fda53df46e5wer45fd1a23f1asfd51safdsfds5f3d1af2a1f22cx21zvb21n11dfsgsd21w515423142614261543w354121sdfd23g1fg13r1fd21w31er1231fs2gfs31g21s43513s1gf3s1g23sg1v21vxc23b1n32m11r6s5t7wt6r7t7412564dfs1sgf3d5g6s4dfs1g3sgf165gf4dfs8g7g8s4g5s1g2';
                
    $passWd $level1paswd."".$pepper;

                
    $queryAdd 'INSERT INTO members (nameFirst,nameLast,password,email,group) VALUES (:Firstname,:Lastname,:password,:email,:group)';

                
    $prepare $con->prepare($queryAdd);
                
    $prepare->execute(array(
                    
    'Firstname' => $this->nameFirst,
                    
    'Lastname' => $this->nameLast,
                    
    'password' => $passWd,
                    
    'email' => $this->email,
                    
    'group' => $this->group)
                );

                echo 
    "<p class='success'>Userid:".$con->LastInsertId()."was sucessfully inserted in the database they can now login in with the password you entred!</p>";
            }

    The resulting echo wasUserid:0was sucessfully inserted in the database they can now login in with the password you entred!

    you have added a user with the name:Matt 

    Ps i am really waiting 
    for the PHP novice to ninja 5th edition book to come out i can not download it yetI figure reading books can not make me a worse coder can it?  

    pps.  this has to be the best community on the web for web department :


  9. #9
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    734
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cmarenburg View Post
    ...but no error is displayed...
    You should make sure your php.ini settings are configured for development: error reporting should be set to all, display errors should be on, startup errors should be on, track errors on, and so forth. The error message might tell you exactly what's going wrong.

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    You should make sure your php.ini settings are configured for development: error reporting should be set to all, display errors should be on, startup errors should be on, track errors on, and so forth. The error message might tell you exactly what's going wrong.
    There is no error message given. And yes i do have errors enabled and all are being shown.

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    the column named 'group' is causing the problem. It is considered a mysql keyword so it's trying to group the result set. That was a real head scratcher lol. If you change it to group_num it should work just fine. Things like this really should generate better errors.

    Also if you use something similar to the following it will allow you to check for a successful query before printing the success message.

    PHP Code:
    try 
            {
                
    $prepare $con->prepare($queryAdd);
                
    $result $prepare->execute(array(
                    
    ':Firstname' => $this->nameFirst,
                    
    ':Lastname' => $this->nameLast,
                    
    ':password' => $passWd,
                    
    ':email' => $this->email,
                    
    ':group' => $this->group)
                );
            }
            catch( 
    PDOException $e )
            {
                echo 
    $e->getMessage();
            }
            
            if( 
    $result )
            {
                echo 
    "<p class='success'>Userid: ".$con->LastInsertId()." was sucessfully inserted in the database they can now login in with the password you entred!</p>";
            } 

  12. #12
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a little FYI, the class name should be Member. If you want to insert a record in the member table, you create a function that does it, ex. insert(). Why? Classes are objects and thus should have a noun like name. Functions are actions so they should use verbs in their names. Plus, if you create a new addMember class, it sounds off.

    PHP Code:
    $bob = new Member(); 
    Bob is a new member. Let's add Bob to the database.

    PHP Code:
    $bob->insert(); 
    Compared to...
    PHP Code:
    $bob = new addMember(); 
    Do you then create editMember and deleteMember as well? You are creating needless classes.

    PHP Code:
    class Member {

        
    // class var
        
    $email null
        
    $nameFirst null
        
    $nameLast null
        
    $pass null
        
    $group null

        public 
    __construct($email$nameFirst$nameLast$pass) {
            
    $this->email $email
            
    $this->nameFirst $nameFirst
            
    $this->nameLast $nameLast
            
    $this->pass =  $pass
            
    $this->group '2';
        }

        public 
    insert () {

        }

        public 
    update () {

        }

        public 
    delete () {

        }

    This would be closer to what I would do. I somewhat skipped officially passing parameters, but you get the idea.

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
  •