Jump to content

[SOLVED] PHP OOP Class Not Working


Akenatehm

Recommended Posts

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
Share on other sites

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
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.