Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.38 KB | None | 0 0
  1. <?php
  2. class user {
  3.     private $database = "sqlite:includes/database/users.sqlite";
  4.     private $db;
  5.     private $data = array();
  6.  
  7.  
  8.     public function __construct($identifier = NULL) {
  9.         $this->db = new PDO($this->database);
  10.  
  11.         if (isset($identifier))
  12.             $this->load($identifier);
  13.     }
  14.  
  15.     public function __get($name) {
  16.         return (isset($this->data[$name]) ? $this->data[$name] : false);
  17.     }
  18.  
  19.     public function __set($name, $value) {
  20.         $this->data[$name] = $value;
  21.     }
  22.  
  23.     public function add() {
  24.         if (isset($this->data['id']))
  25.             return false;
  26.  
  27.         //diese hier brauchen wir unbedingt, sonst kann der user nicht geaddet werden
  28.         $required = array('name', 'email', 'password');
  29.         foreach ($required as $attribute) {
  30.             if (!isset($this->data[$attribute]))
  31.                 return false;
  32.         }
  33.  
  34.         //Encrypt password
  35.         $this->data['password'] = md5($this->data['password']);
  36.  
  37.         //Prepare SQL query string
  38.         $attributes = '';
  39.         $values = '';
  40.         foreach ($this->data as $attribute => $value) {
  41.             $attributes .= $attribute . ', ';
  42.             $values .= ':' . $attribute . ', ';
  43.         }
  44.         $attributes = trim($attributes, ', ');
  45.         $values = trim($values, ', ');
  46.  
  47.         $query = 'INSERT INTO users (' . $attributes . ') VALUES (' . $values . ')';
  48.         var_dump($query);
  49.  
  50.         $db_prep = $this->db->prepare($query);
  51.         foreach ($this->data as $attribute => $value) {
  52.             $db_prep->bindParam(":$attribute", $value);
  53.         }
  54.  
  55.         //ne gute idee um erfolg zu checken:
  56.         return $db_prep->execute();
  57.     }
  58.  
  59.     public function load($identifier) {
  60.         if(is_numeric($identifier)) {
  61.             //Get assoc array
  62.             $db_prep = $this->db->prepare("SELECT * FROM users WHERE id = :id");
  63.             $db_prep->bindParam(":id",$identifier);
  64.             $db_prep->execute();
  65.             $this->data = $db_prep->fetch(PDO::FETCH_ASSOC);
  66.             return true;
  67.         }
  68.         else {
  69.             return false;
  70.         }
  71.     }
  72.    
  73.     //sollte nur nach load ausgeführt werden
  74.     public function save() {
  75.         if (!isset($this->data['id']))
  76.             return false;
  77.  
  78.         //Encrypt password
  79.         if (isset($this->data['password']))
  80.             $this->data['password'] = md5($this->data['password']);
  81.  
  82.         //Prepare SQL query string
  83.         $attributes = '';
  84.         foreach ($this->data as $attribute => $value) {
  85.             $attributes .= $attribute . ' = :' . $attribute .  ', ';
  86.         }
  87.         $attributes = trim($attributes, ', ');
  88.  
  89.         $query = 'UPDATE users SET ' . $attributes . ' WHERE id = ' . $this->data['id'];
  90.  
  91.         $db_prep = $this->db->prepare($query);
  92.         foreach ($this->data as $attribute => $value) {
  93.             $db_prep->bindParam(':' . $attribute, $value);
  94.         }
  95.  
  96.         //ne gute idee um erfolg zu checken:
  97.         return $db_prep->execute();
  98.  
  99.     }
  100. }
  101.  
  102. ?>
  103.  
  104. // Code to call the class
  105.  
  106. <?php
  107. error_reporting(E_ALL);
  108. ini_set('display_errors','On');
  109. include("includes/classes.php");
  110. $user = new user();
  111. $user->name = 'arez';
  112. $user->password = 'lalala';
  113. $user->email = 'noone@home.com';
  114. $user->age = '44';
  115. $user->add();
  116.  
  117. ?>
  118.  
  119. // Error
  120. 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