Stumped OOP php

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Tue Nov 27, 2012 11:41 am

Sounds like you've kind of bitten off a little more than you can currently chew on this one, but don't fret you can get up to speed pretty quickly. There are several ways to approach your particular situation, I recommend going to http://jream.com/learning/videos/php-oop for a quick crash course. Free video lessons there for php oop.

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Wed Nov 28, 2012 11:55 pm

I know your struggle as I had the same problems when starting out years ago - no 'real world' examples on how to implement what you are learning.

To keep things simple, this is what you need, very simply.

Code: Select all

// You need to extend your connection class (if it is a class) so that you can connect to the db
class BlogUser extends Connection {
protected $User_name;
protected $User_email;
protected $User_password;
protected $User_signup_date;

function __construct() {
  // This initiates your class and you can put variables or call functions in here that always need running when your class is instantiated (if any, or you can leave blank)
// For instance connect to the database (I would need to see your connection class to help with this)
}

function add_user() {
  $query = "INSERT INTO users (username, hashed_password) VALUES ('".$this->User_name."', '".$this->User_password."')";
  $result = mysql_query($query, $connection);
  if ($result) {
    $this->message = "The user was successfully created.";
  } else {
    $this->message = "The user could not be created.";
    $this->message .= "<br />" . mysql_error();
  }
  return $this->message;
}
In your page:

Code: Select all

if ( empty($errors) ) {
$password = trim(mysql_prep($_POST['password']));

$user = new BlogUser;  
$user->User_name = trim(mysql_prep($_POST['username']));
$user->User_password = sha1($password);

$user->add_user();

echo $user->message;
}
I hope this gives you a better idea and you can apply it to the other code/classes.

Post Reply