Jragon Posted June 2, 2011 Share Posted June 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/ Share on other sites More sharing options...
fugix Posted June 2, 2011 Share Posted June 2, 2011 in your index.php page...why are you including require_once('includes/include.php'); instead of your class.php and connect.php? Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224171 Share on other sites More sharing options...
gristoi Posted June 2, 2011 Share Posted June 2, 2011 in your index add an additional error handler: try { $user = new user(); $user->newUser('Jragon', 'blenders', $DBH); } catch(PDOException $e) { var_dump($e); } Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224172 Share on other sites More sharing options...
Jragon Posted June 2, 2011 Author Share Posted June 2, 2011 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224173 Share on other sites More sharing options...
Jragon Posted June 2, 2011 Author Share Posted June 2, 2011 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(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224174 Share on other sites More sharing options...
fugix Posted June 2, 2011 Share Posted June 2, 2011 did you see my post Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224190 Share on other sites More sharing options...
Jragon Posted June 2, 2011 Author Share Posted June 2, 2011 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224192 Share on other sites More sharing options...
Jragon Posted June 2, 2011 Author Share Posted June 2, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224197 Share on other sites More sharing options...
Jragon Posted June 2, 2011 Author Share Posted June 2, 2011 Do you have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224231 Share on other sites More sharing options...
gristoi Posted June 2, 2011 Share Posted June 2, 2011 The error handler telling you that you don't have a table called ranks. Quote Link to comment https://forums.phpfreaks.com/topic/238219-pdo-and-oop-not-updating-database/#findComment-1224233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.