Akenatehm Posted June 30, 2009 Share Posted June 30, 2009 Hey Guys, For some reason, I don't know why this is displaying errors. Help would be appreciated if possible. Member.class.php <?php class Register{ private $dbhost,$dbuser,$dbpass,$database,$LastResult; public function __construct($dbhost,$dbuser,$dbpass,$database) { $this->dbhost = $dbhost; $this->dbuser = $dbuser; $this->dbpass = $dbpass; $this->database = $database; } public function checkUsername($username){ $this->username = mysql_escape_string($username); $connect = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die(mysql_error()); mysql_select_db($this->database) or die(mysql_error()); $checkuser= mysql_query("SELECT * FROM users WHERE Username = '$this->username'") or die(mysql_error()); if(mysql_num_rows($checkuser) == 1){ $response = "Username Already Exists <br />"; } mysql_close($connect); } public function samePasswords($password,$confirmpassword){ $this->password = md5(mysql_escape_string($password)); $this->confirmpassword = md5(mysql_escape_string($confirmpassword)); if($this->password != $this->confirmpassword){ $response = "Passwords Do Not Match<br />"; } } public function checkEmail($email) { $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; $check = (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); if($check == 'false'){ $response = "Invalid Email<br />"; } } public function createUser($username,$password,$email){ $connect = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die(mysql_error()); mysql_select_db($this->database) or die(mysql_error()); $insertuser = mysql_query("INSERT into users (username,password,email) values('$username','$password','$email')") or die(mysql_error()); if($insertuser){ $response = "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>"; } elseif(!$insertuser){ $response = "Problem Creating User. Please Try Again Later.<br />"; } mysql_close($connect); } } ?> register.php: <?php session_start(); require "Connection.class.php"; require 'Member.class.php'; $select = new Connection('localhost','root',''); $select->db ('TimelessVoice'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Management System (Tom Cameron for NetTuts)</title> <link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" /> <script src="jquery.js" type="text/javascript"></script> <script src="validate/cmxforms.js" type="text/javascript"></script> <script type="text/javascript" src="validate/jquery.validate.js"></script> <script type="text/javascript"> $().ready(function() { // validate signup form on keyup and submit $("#registerform").validate({ rules: { username: { required: true, minlength: 5 }, password: { required: true, minlength: 5 }, confirmpassword: { required: true, minlength: 5, equalTo: "#password" }, email: { required: true, email: true }, messages: { username: { required: "Please enter a username", minlength: "Your username must consist of at least 5 characters" }, password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long" }, confirm_password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long", equalTo: "Please enter the same password as above" }, email: "Please enter a valid email address" } } }); }); // check if confirm password is still valid after password changed $("#password").blur(function() { $("#confirmpassword").valid(); }); </script> </head> <body> <div id="main"> <?php if(!empty($_POST['username']) && !empty($_POST['password'])) { $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $database = 'TimelessVoice'; $servervalidation = new Register($dbhost,$dbuser,$dbpass,$database); if(!$servervalidation->checkEmail($_POST['email'])) { echo "<h1>Error1</h1>"; echo $servervalidation->checkEmail->$response; } elseif(!$servervalidation->checkUsername($_POST['username'])) { echo "<h1>Error2</h1>"; echo $servervalidation->checkUsername->$response; } elseif(!$servervalidation->samePasswords($_POST['password'],$_POST['confirmpassword'])) { echo "<h1>Error3</h1>"; echo $servervalidation->samePasswords->$response; } elseif(!$servervalidation->checkEmail($_POST['email'])){ echo "<h1>Error4</h1>"; echo $servervalidation->checkEmail->$response; } else{ $servervalidation->createUser($servervalidation->username,$servervalidation->password,$servervalidation->email); echo $servervalidation->createUser->$response; } } else{ ?> <h1>Register</h1> <p>Please enter your details below to register.</p> <form method="post" action="register.php" name="registerform" id="registerform" class="cmxform"> <fieldset> <label for="username">Username:</label><input type="text" name="username" id="username" /><br /> <label for="password">Password:</label><input type="password" name="password" id="password" /><br /> <label for="confirmpassword">Confirm Password:</label><input type="password" name="confirmpassword" id="confirmpassword" /><br /> <label for="email">Email Address:</label><input type="text" name="email" id="email" /><br /> <input type="submit" name="register" id="register" value="Register" /> </fieldset> </form> <?php } ?> </div> </body> </html> It is outputting "Error1" with no other message. Thanks in Advanced, Cody Link to comment https://forums.phpfreaks.com/topic/164215-solved-php-oop-class-not-working/ Share on other sites More sharing options...
Akenatehm Posted June 30, 2009 Author Share Posted June 30, 2009 Anyone? Link to comment https://forums.phpfreaks.com/topic/164215-solved-php-oop-class-not-working/#findComment-866237 Share on other sites More sharing options...
Adam Posted June 30, 2009 Share Posted June 30, 2009 Well the code generating the error is: if(!$servervalidation->checkEmail($_POST['email'])) { echo "<h1>Error1</h1>"; echo $servervalidation->checkEmail->$response; } Obviously it's calling the 'checkEmail' method, which must be returning false. Try changing it to: public function checkEmail($email) { $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; if (preg_match($formatTest, $email) && preg_match($lengthTest, $email)) { return true; } else { return false; } } I've not validated the regex used by the way, there could possibly be an error there. Link to comment https://forums.phpfreaks.com/topic/164215-solved-php-oop-class-not-working/#findComment-866244 Share on other sites More sharing options...
trq Posted June 30, 2009 Share Posted June 30, 2009 You might want to take a look at how to set and retrieve object properties. Your methods set a variable ($reponse) which is local only to the method it is set it. Link to comment https://forums.phpfreaks.com/topic/164215-solved-php-oop-class-not-working/#findComment-866245 Share on other sites More sharing options...
Akenatehm Posted June 30, 2009 Author Share Posted June 30, 2009 Hey Guys, Thanks to your help, I managed to fix some things and it all works fine. Thanks a lot. Much appreciated! Cody Link to comment https://forums.phpfreaks.com/topic/164215-solved-php-oop-class-not-working/#findComment-866251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.