Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class user {
- private $database = "sqlite:includes/database/users.sqlite";
- private $db;
- private $data = array();
- public function __construct($identifier = NULL) {
- $this->db = new PDO($this->database);
- if (isset($identifier))
- $this->load($identifier);
- }
- public function __get($name) {
- return (isset($this->data[$name]) ? $this->data[$name] : false);
- }
- public function __set($name, $value) {
- $this->data[$name] = $value;
- }
- public function add() {
- if (isset($this->data['id']))
- return false;
- //diese hier brauchen wir unbedingt, sonst kann der user nicht geaddet werden
- $required = array('name', 'email', 'password');
- foreach ($required as $attribute) {
- if (!isset($this->data[$attribute]))
- return false;
- }
- //Encrypt password
- $this->data['password'] = md5($this->data['password']);
- //Prepare SQL query string
- $attributes = '';
- $values = '';
- foreach ($this->data as $attribute => $value) {
- $attributes .= $attribute . ', ';
- $values .= ':' . $attribute . ', ';
- }
- $attributes = trim($attributes, ', ');
- $values = trim($values, ', ');
- $query = 'INSERT INTO users (' . $attributes . ') VALUES (' . $values . ')';
- var_dump($query);
- $db_prep = $this->db->prepare($query);
- foreach ($this->data as $attribute => $value) {
- $db_prep->bindParam(":$attribute", $value);
- }
- //ne gute idee um erfolg zu checken:
- return $db_prep->execute();
- }
- public function load($identifier) {
- if(is_numeric($identifier)) {
- //Get assoc array
- $db_prep = $this->db->prepare("SELECT * FROM users WHERE id = :id");
- $db_prep->bindParam(":id",$identifier);
- $db_prep->execute();
- $this->data = $db_prep->fetch(PDO::FETCH_ASSOC);
- return true;
- }
- else {
- return false;
- }
- }
- //sollte nur nach load ausgeführt werden
- public function save() {
- if (!isset($this->data['id']))
- return false;
- //Encrypt password
- if (isset($this->data['password']))
- $this->data['password'] = md5($this->data['password']);
- //Prepare SQL query string
- $attributes = '';
- foreach ($this->data as $attribute => $value) {
- $attributes .= $attribute . ' = :' . $attribute . ', ';
- }
- $attributes = trim($attributes, ', ');
- $query = 'UPDATE users SET ' . $attributes . ' WHERE id = ' . $this->data['id'];
- $db_prep = $this->db->prepare($query);
- foreach ($this->data as $attribute => $value) {
- $db_prep->bindParam(':' . $attribute, $value);
- }
- //ne gute idee um erfolg zu checken:
- return $db_prep->execute();
- }
- }
- ?>
- // Code to call the class
- <?php
- error_reporting(E_ALL);
- ini_set('display_errors','On');
- include("includes/classes.php");
- $user = new user();
- $user->name = 'arez';
- $user->password = 'lalala';
- $user->email = 'noone@home.com';
- $user->age = '44';
- $user->add();
- ?>
- // Error
- Fatal error: Call to a member function bindParam() on a non-object in ... on line 52
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement