Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.84 KB | None | 0 0
  1. <?php
  2.     include 'config_fns.php';
  3.    
  4.     $connect = new MySql();
  5.     $connect->connect($db_host, $db_username, $db_password, $db_name);
  6.    
  7.     class MySql
  8.     {
  9.         var  $host;
  10.         var  $user;
  11.         var  $password;
  12.         var  $database;
  13.         public  $db_handle;
  14.        
  15.         public function connect($db_host, $db_user, $db_password, $db_database)
  16.         {
  17.             $this->host = $db_host;
  18.             $this->user = $db_user;
  19.             $this->password = $db_password;
  20.             $this->database = $db_database;
  21.            
  22.             $this->db_handle = mysql_connect($this->host, $this->user, $this->password)or die('could not connect to db');
  23.             mysql_select_db($this->database)or die('could not select db');
  24.         }
  25.        
  26.         public function query($sql)
  27.         {
  28.             return mysql_query($sql, $this->db_handle) or die (mysql_error());
  29.         }
  30.        
  31.         public function fetch($result)
  32.         {
  33.             return mysql_fetch_assoc($result, $this->db_handle) or die (mysql_error());
  34.         }
  35.        
  36.     }
  37.    
  38.     class User
  39.     {
  40.         public $db;
  41.         public $id;
  42.         public $username;
  43.         public $password;
  44.        
  45.         public function __construct($db, $id = 0)
  46.         {
  47.             $this->db = $db;
  48.             $this->id = $id;
  49.            
  50.             if($this->$id !== 0)
  51.             {
  52.                 $result = mysql_query("SELECT `username`, `password` FROM `user` WHERE `id` = $this->id");
  53.                 $row = mysql_fetch_assoc($result);
  54.                
  55.                    
  56.                     if($row)
  57.                     {
  58.                        
  59.                         $this->username = $row['username'];
  60.                         $this->password = $row['password'];
  61.                     }
  62.                
  63.                 return $row;
  64.                    
  65.                    
  66.                
  67.                
  68.                 echo mysql_error();
  69.             }
  70.         }
  71.        
  72.         public function setUsername($name)
  73.         {
  74.             $this->username = $name;
  75.             return $this;
  76.         }
  77.        
  78.         public function setPassword($password)
  79.         {
  80.             $this->password = $password;
  81.             return $this;
  82.         }
  83.        
  84.         public function getUsername()
  85.         {
  86.             return $this->username;
  87.         }
  88.        
  89.         public function getPassword()
  90.         {
  91.             return $this->password;
  92.         }
  93.        
  94.         public function save()
  95.         {
  96.             if($this->id == 0)
  97.             {
  98.                 $id = $this->db->query("INSERT INTO `user` (username, password) VALUES ('{$this->username}', '{$this->password}')");
  99.                 $this->id = $this -> id = mysql_insert_id ( );
  100.             }else{
  101.                 $this->db->query("UPDATE `user` SET `username` = '{$this->username}', `password` = '{$this->password}' WHERE `id` = '{$this->id}'");
  102.             }
  103.             return $this->id;
  104.         }
  105.     }
  106.    
  107.     $user = new User($connect, 1);
  108.     echo $user->getUsername() . '<br/>';
  109.     echo $user->getPassword();
  110.    
  111.    
  112.     $user = new User($connect, 1);
  113.     $user->setUsername($_POST['username']);
  114.     $user->setPassword($_POST['password']);
  115.     $id = $user->save();
  116.    
  117.    
  118.     //echo "<br/> New user {$user->getUsername()} created with a password of {$user->getPassword()} and an id of $id";
  119.    
  120. ?>
  121.  
  122. <form method="POST" target="self">
  123.     <input type="text" name="username" value="Username" onclick="this.value = ''" /><br/>
  124.     <input type="text" name="password" value="Password" onclick="this.value = ''" /><br/>
  125.     <input type="submit" name="submit" />
  126. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement