Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.61 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 = $this->db->query("SELECT `username`, `password` FROM `user` WHERE `id` = $this->id");
  53.                 $row = $this->db->fetch($result);
  54.                    
  55.                     if($result)
  56.                     {
  57.                        
  58.                         $this->username = $result->username;
  59.                         $this->password = $result->password;
  60.                     }
  61.                
  62.                 //return $row;
  63.                    
  64.                    
  65.                
  66.                
  67.                 echo mysql_error();
  68.             }
  69.         }
  70.        
  71.         public function setUsername($name)
  72.         {
  73.             $this->username = $name;
  74.             return $this;
  75.         }
  76.        
  77.         public function setPassword($password)
  78.         {
  79.             $this->password = $password;
  80.             return $this;
  81.         }
  82.        
  83.         public function getUsername()
  84.         {
  85.             return $this->username;
  86.         }
  87.        
  88.         public function getPassword()
  89.         {
  90.             return $this->password;
  91.         }
  92.        
  93.         public function save()
  94.         {
  95.             if($this->id == 0)
  96.             {
  97.                 $id = $this->db->query("INSERT INTO `user` (username, password) VALUES ('{$this->username}', '{$this->password}')");
  98.                 $this->id = $this -> id = mysql_insert_id ( );
  99.             }else{
  100.                 $this->db->query("UPDATE `user` SET `username` = '{$this->username}', `password` = '{$this->password}' WHERE `id` = '{$this->id}'");
  101.             }
  102.             return $this->id;
  103.         }
  104.     }
  105.    
  106.     /*
  107.     $user = new User($connect, 31);
  108.     $user->setUsername($_POST['username']);
  109.     $user->setPassword($_POST['password']);
  110.     $id = $user->save();
  111.     */
  112.        
  113.     $user = new User($connect, 31);
  114.     echo $user->getUsername();
  115.     print_r($user);
  116.    
  117.  
  118.     //echo "<br/> New user {$user->getUsername()} created with a password of {$user->getPassword()} and an id of $id";
  119.    
  120.     /*class User
  121.     {
  122.         public $username;
  123.         public $password;
  124.        
  125.         public function setUsername($name)
  126.         {
  127.             $this->username = $name;
  128.         }
  129.        
  130.         public function setPassword($password)
  131.         {
  132.             $this->password = $password;
  133.         }
  134.        
  135.         public function query($sql)
  136.         {
  137.             $sql = "INSERT INTO `user` (`username`, `password`) VALUES ({$CreateUser['username']}, {$CreateUser['password']})";
  138.         }
  139.     }
  140.    
  141.     $newUser = new CreateUser();
  142.     $newUser->setUsername($_POST['username']);
  143.     $newUser->setPassword($_POST['password']);
  144.     $connect->query("INSERT INTO `user` (`username`, `password`, created_at) VALUES ('$newUser->username', '$newUser->password', NOW())");
  145.     //$connect->query("SELECT * FROM `users`");
  146.     //$connect->fetch($MySql->query);
  147.     echo mysql_error() . '<br/>';
  148.     print_r($newUser);
  149.     */
  150. ?>
  151.  
  152. <form method="POST" target="self">
  153.     <input type="text" name="username" value="Username" onclick="this.value = ''" /><br/>
  154.     <input type="text" name="password" value="Password" onclick="this.value = ''" /><br/>
  155.     <input type="submit" name="submit" />
  156. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement