Jump to content

PDO, and OOP, not updating database


Jragon

Recommended Posts

Hey guys,

 

I've been doing a little bit of exerementing with PDO's and it seems I can't get it to update the database... I don't know why...

 

Anyway here is the code:

 

index.php

<?php

/**
* @author Jragon
* @copyright 2011
*/

require_once('includes/include.php');

$user = new user();
$user->newUser('Jragon', 'blenders', $DBH);

echo '=D';

?>

 

/includes/connect.inc.php

<?php

/**
* @author Jragon
* @copyright 2011
*/


//Define connection details

//host
define('HOST', 'localhost');

//DataBase
define('DB', 'rankometer');

//Username
define('USER', 'root');

//Password
define('PASS', '');


?>

 

 

/includes/connect.php

<?php

/**
* @author Jragon
* @copyright 2011
*/

//Include connection crap
require_once('connect.inc.php');


//connect
try {
    //new PDO
    $DBH = new PDO("mysql:host=" . HOST . ";dbname=" . DB, USER, PASS);  
//check for errors
} catch(PDOException $e) {
    echo $e->getMessage();  
}  
?>

 

/classes/user.class.php

<?php

/**
* @author Jragon
* @copyright 2011
*/

class user{

    private $username;
    private $password;
    private $newPassword;
    private $salt;
    private $DBH;

    
    public function newUser($user, $pass, $DBH){
        //name varibles
        $this->password = $pass;
        $this->username = $user;
        $this->DBH = $DBH;
        
        $this->getSalt();
        $this->hashPass();
        $this->createUser();
        
    }
    
    private function getSalt(){
        //pick random number
        $a = rand(1, 100);
        $b = rand(1, 100);
        $this->salt = rand($a, $b) * 5;
    }
    
    private function hashPass(){
        //encrypt $this->password with md5 and a salt
        $plainPass = $this->password . $this->salt;
        $this->newPassword = md5($plainPass);
    }
    
    private function createUser(){
        //prepare statement
        $STH = $this->DBH->prepare("
        INSERT INTO `users` (
        `user` ,
        `pass` ,
        `salt`
        )
        VALUES (
        '$this->username', '$this->newPassword', '$this->salt'
        );
        ");  
        //execute statement
        $STH->execute();  
        
        //prepare statement
        $STH = $this->DBH->prepare("
        INSERT INTO `ranks` (
        `rank`
        )
        VALUES (
        
        );
        ");  
        //execute statement
        $STH->execute();  
        
    }
    
    
    
}

?>

 

SQL dump

-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 02, 2011 at 06:05 PM
-- Server version: 5.5.8
-- PHP Version: 5.3.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `cakes`
--

-- --------------------------------------------------------

--
-- Table structure for table `cakes`
--

CREATE TABLE IF NOT EXISTS `cakes` (
  `UID` int(11) NOT NULL AUTO_INCREMENT,
  `cake` int(11) NOT NULL,
  PRIMARY KEY (`UID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `cakes`
--


-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `UID` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(11) NOT NULL,
  `passWord` varchar(32) NOT NULL,
  `salt` int(11) NOT NULL,
  PRIMARY KEY (`UID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `users`
--

 

I have no idea what is going wrong... I don't get an error. All the output I get is '=D'.

 

Thanks

 

-Jragon

Link to comment
Share on other sites

Oops, I forgot to add the includes file...

 

/includes/include.php

<?php

/**
* @author Jragon
* @copyright 2011
*/

require_once('connect.inc.php'); 
require_once('connect.php');
require_once('./classes/user.class.php');



?>

Link to comment
Share on other sites

Still no errors, I also added some error stuff in user.class.php

 

<?php

/**
* @author Jragon
* @copyright 2011
*/

class user
{

    private $username;
    private $password;
    private $newPassword;
    private $salt;
    private $DBH;


    public function newUser($user, $pass, $DBH)
    {
        //name varibles
        $this->password = $pass;
        $this->username = $user;
        $this->DBH = $DBH;

        $this->getSalt();
        $this->hashPass();
        $this->createUser();

    }

    private function getSalt()
    {
        //pick random number
        $a = rand(1, 100);
        $b = rand(1, 100);
        $this->salt = rand($a, $b) * 5;
    }

    private function hashPass()
    {
        //encrypt $this->password with md5 and a salt
        $plainPass = $this->password . $this->salt;
        $this->newPassword = md5($plainPass);
    }

    private function createUser()
    {
        /*
        
        //prepare statement
        $STH = $this->DBH->prepare("
        INSERT INTO `users` (
        `user` ,
        `pass` ,
        `salt`
        )
        VALUES (
        '$this->username', '$this->newPassword', '$this->salt'
        );
        ");  
        //execute statement
        $STH->execute();  
        
        //prepare statement
        $STH = $this->DBH->prepare("
        INSERT INTO `ranks` (
        `rank`
        )
        VALUES (
        
        );
        ");  
        //execute statement
        $STH->execute();  
        
        */

        $STH = $this->DBH->prepare("
            INSERT INTO `users` (
                `user` ,
                `pass` ,
                `salt`
            )
            VALUES (
                '$this->username', '$this->newPassword', '$this->salt'
            );
            ");
        //execute statement
        if (!$STH)
        {
            print_r($DBH->errorInfo());
            die();
        }

        $STH->execute();

        //prepare statement
        $STH = $this->DBH->prepare("
                INSERT INTO `ranks` (
                `rank`
                )
                VALUES (
                0
                );
            ");

        if (!$STH)
        {
            print_r($DBH->errorInfo());
            die();
        }
        //execute statement
        $STH->execute();


    }


}

?>

Link to comment
Share on other sites

I did...

 

I forgot to add /includes/include.php

 

/includes/include.php

<?php

/**
* @author Jragon
* @copyright 2011
*/

require_once('connect.inc.php'); 
require_once('connect.php');
require_once('./classes/user.class.php');



?>

Link to comment
Share on other sites

Ok.. I found out it was the MySQL =S... I fixed it.. And now I have a whole load of errors

 

object(PDOException)#3 ( { ["message":protected]=> string(85) "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cakes.ranks' doesn't exist" ["string":"Exception":private]=> string(0) "" ["code":protected]=> string(5) "42S02" ["file":protected]=> string(49) "C:\xampp\htdocs\rankonator\classes\user.class.php" ["line":protected]=> int(116) ["trace":"Exception":private]=> array(3) { [0]=> array(6) { ["file"]=> string(49) "C:\xampp\htdocs\rankonator\classes\user.class.php" ["line"]=> int(116) ["function"]=> string(7) "execute" ["class"]=> string(12) "PDOStatement" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [1]=> array(6) { ["file"]=> string(49) "C:\xampp\htdocs\rankonator\classes\user.class.php" ["line"]=> int(27) ["function"]=> string(10) "createUser" ["class"]=> string(4) "user" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(36) "C:\xampp\htdocs\rankonator\index.php" ["line"]=> int(12) ["function"]=> string(7) "newUser" ["class"]=> string(4) "user" ["type"]=> string(2) "->" ["args"]=> array(3) { [0]=> string(6) "Jragon" [1]=> string( "blenders" [2]=> object(PDO)#1 (0) { } } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> array(3) { [0]=> string(5) "42S02" [1]=> int(1146) [2]=> string(33) "Table 'cakes.ranks' doesn't exist" } } =D

 

here is the updated sql


-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 02, 2011 at 07:10 PM
-- Server version: 5.5.8
-- PHP Version: 5.3.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `cakes`
--

-- --------------------------------------------------------

--
-- Table structure for table `cakes`
--

CREATE TABLE IF NOT EXISTS `cakes` (
  `UID` int(11) NOT NULL AUTO_INCREMENT,
  `cake` int(11) NOT NULL,
  PRIMARY KEY (`UID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `cakes`
--


-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `UID` int(11) NOT NULL AUTO_INCREMENT,
  `user` varchar(11) NOT NULL,
  `pass` varchar(32) NOT NULL,
  `salt` int(11) NOT NULL,
  PRIMARY KEY (`UID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`UID`, `user`, `pass`, `salt`) VALUES
(2, 'Jragon', '82ee1cd9be48fb781f40456fbd0ffef9', 205);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.