Guest User

UserModel.php

a guest
Apr 3rd, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Model;
  4. use PDO;
  5.  
  6. class AddUserModel {
  7.     // Function to add users to the database
  8.     public  function addUser(){
  9.  
  10.         try {
  11.  
  12.             $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
  13.             $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
  14.  
  15.             $password = hash('sha256', $password);
  16.  
  17.  
  18.             require CONTROLLER_DIR . '/dbConnecterController.php';
  19.  
  20.             $result = $db->query('SELECT * FROM users WHERE username="'.$username.'"');
  21.  
  22.             if ($username && $password) {
  23.  
  24.                 $row = $result->fetchColumn();
  25.                 if($row == 0){
  26.                     $statement = $db->query("INSERT INTO users (username,password) VALUES ('$username', '$password')" );
  27.                     $db = null;
  28.                     return true;
  29.                 } else {
  30.                     die("Username already taken, pick something else! <br><a href='/userlist'> Go back to user list");
  31.                 }  
  32.             }
  33.  
  34.         } catch (PDOException $e) {
  35.             print "Error!: " . $e->getMessage() . "<br/>";
  36.             die();
  37.         }
  38.     }
  39.  
  40.  
  41.     // Function to display the users inside a table
  42.     public function showUsers(){
  43.  
  44.         try{
  45.             require CONTROLLER_DIR . '/dbConnecterController.php';
  46.  
  47.             $result = $db->query('SELECT id, username FROM users ORDER BY id');
  48.  
  49.  
  50.             echo "<table border='1'>
  51.                 <tr>
  52.                 <th>ID</th>
  53.                 <th>Username</th>
  54.                 <th></th>
  55.                 <th></th>
  56.                 </tr>";
  57.             // This will make it so admin is not displayed on the table
  58.             $row = $result->fetchColumn();
  59.             while($row = $result->fetch(PDO::FETCH_ASSOC)){
  60.                 echo "<tr>";
  61.                 echo "<td>" . $row['id'] . "</td>";
  62.                 echo "<td>" . $row['username'] . "</td>";
  63.                 echo '<td>
  64.                         <form method="GET" action="/showEditUserPage">
  65.                             <input type="hidden" name="id" value="' . $row['id'] . '"/>
  66.                             <input type="hidden" name="username" value="' . $row['username'] . '"/>
  67.                             <input type="submit" value="EDIT" name="showEditUserPage"/>
  68.                         </form></td>';
  69.                 echo '<td>
  70.                         <form method="POST" action="/deleteUser">
  71.                             <input type="hidden" name="id" value="' . $row['id'] . '"/>
  72.                             <input type="submit" value="DELETE" name="deleteUser"/>
  73.                         </form></td>';
  74.                 echo "</tr>";
  75.             }
  76.             echo '</table>';
  77.  
  78.         } catch (PDOException $e) {
  79.             print "Error!: " . $e->getMessage() . "<br/>";
  80.             die();
  81.         }
  82.     }
  83.  
  84.     // Function to delete users from the database
  85.     public function deleteUser(){
  86.         $id = $_POST["id"];
  87.         try {
  88.             require CONTROLLER_DIR . '/dbConnecterController.php';
  89.             $result = $db->query('SELECT * FROM users WHERE id="'.$id.'"');
  90.  
  91.             $row = $result->fetchColumn();
  92.                 if(!$row == 0){
  93.                     $statement = $db->query('DELETE FROM users WHERE id="'.$id.'" ');
  94.                     $db = null;
  95.                     return true;
  96.                 } else {
  97.                     die("No data present.");
  98.                 }  
  99.  
  100.         } catch (PDOException $e) {
  101.             print "Error!: " . $e->getMessage() . "<br/>";
  102.             die();
  103.         }
  104.     }
  105.  
  106.     // Function to update users
  107.     public function editUser(){
  108.  
  109.  
  110.         $username = $_POST["username"];
  111.         $id = $_POST["id"];
  112.         try {
  113.             require CONTROLLER_DIR . '/dbConnecterController.php';
  114.             $result = $db->query('SELECT * FROM users WHERE username="'.$username.'"');
  115.  
  116.             $row = $result->fetchColumn();
  117.                 if(!$row == 0){
  118.                     // To be continued
  119.                     $statement = $db->query("UPDATE users SET username ='$username' WHERE id = '$id'");
  120.                     $db = null;
  121.                     return true;
  122.                 } else {
  123.                     die("No data present.");
  124.                 }  
  125.  
  126.         } catch (PDOException $e) {
  127.             print "Error!: " . $e->getMessage() . "<br/>";
  128.             die();
  129.         }
  130.     }
  131. }
Add Comment
Please, Sign In to add comment