Jump to content

more OOP help?


Spring

Recommended Posts

Here's the register page:

<?php
include ("database.php");
include ("class.register.php");

echo '<html>
<title>Register</title>
<body>
</body>

<form method="post" action="">
  Username: <input type="username" name="username" size="15" /><br />
  Password: <input type="password" name="password" size="15" /><br />
  <div align="center">
    <p><input type="submit" name = "submit" value="Login" /></p>
  </div>
</form>

</html>';
if(isset($_POST['submit']))
{
$reg = new register($_POST['username'], $_POST['password']);
$reg->checkEmpty();

}

?>

 

 

Here's the class im working on

<?php

class register{

//Setting up Variables, you can add more, for example EMAIL.

private $_username;
private $_password;

//Giving the variables values
function __construct($u_name, $p_word)
{
    $this->_username = $u_name;
    $this->_password = $p_word;
   

}

//If you want to do some error checking or whatever
public function getUsername() {
    return $this->_username;
  }

//checks to make sure fields are set
public function checkEmpty()
{
  if(empty($u_name) || empty($p_word)){
    echo "All fields are required to continue!";
  
  }
else
{
     $this->_mysql_check();
}

}

private function _mysql_check()
{
  $query = mysql_query("SELECT username FROM acc WHERE username = '$u_name'") . mysql_error();
  if (mysql_num_rows($query) != 0) {
        echo "Username already exists!";
        }
   else
   {
     $this->_msql_insert();   
    }

}

private function _mysql_insert()
{
  $query = mysql_query("INSERT INTO acc ('username', 'password') VALUES ('$u_name',$p_word'") . mysql_error();
}



}









?>

 

For some reason I keep getting All fields are required to continue! Even if I fill out both fields. Can anyone help?

Link to comment
Share on other sites

$u_name & p_word do not exist within the checkEmpty() method.

 

ps: Just because your using classes doe snot mean your code is OOP. This is fare from well designed code. You are creating new Users so you should likely have a User class. The form validation stuff belong on a form validation class, it has nothing to do with creating a new User.

Link to comment
Share on other sites

Building on what thorpe said, this isn't well designed. However, in terms of maintainability and reusability it is better than nothing. All you have to do is include your class and feed it username and password - build in some functions for setting table name and connection settings and it's self sufficient.

 

Again though, that is bad design. You should have a class for:

 

database connection

user mapping

validation

 

..at very least. Where each of those classes will actually extend abstract classes.

 

Your checkEmpty() method I wouldn't consider form validation at all. You're checking if two parameters exist or not, those could come from anywhere.

 

The problem here is that you're given so much flexibility in how you structure things it's sometimes hard to decide the best way. My advice is to download a decent OOP based framework and have a look at how they structure their code (specifically, database connection and interaction with tables etc).

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.