Advertisement
Guest User

Category.php (Model)

a guest
Feb 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2.  
  3. class Category {
  4.  
  5.     // database connection and table name
  6.     private $conn;
  7.     private $table_name = "kategori";
  8.  
  9.     // object properties
  10.     public $ID;
  11.     public $katNavn;
  12.  
  13.     // constructor with $db as database connection
  14.     public function __construct($db) {
  15.         $this->conn = $db;
  16.     }
  17.  
  18.     // read products
  19.     function read($id) {
  20.         $query = "SELECT * FROM " . $this->table_name;
  21.        
  22.         if($id != '-1') {
  23.             // select query
  24.             $query .= " WHERE kategori.ID = ".$id;
  25.         }
  26.         // prepare query statement
  27.         $stmt = $this->conn->prepare($query);
  28.    
  29.         // execute query
  30.         $stmt->execute();
  31.    
  32.         return $stmt;
  33.     }
  34.    
  35.     function write($post) {
  36.         $katNavn = $post['katNavn'];
  37.         $query = "INSERT INTO " . $this->table_name . " (katNavn) VALUES " .
  38.         "('$katNavn');";
  39.        
  40.         // prepare query statement
  41.         $stmt = $this->conn->prepare($query);
  42.  
  43.         try {
  44.             // execute query
  45.             return $stmt->execute();
  46.         } catch (PDOException $e) {
  47.             return ($e);
  48.         }
  49.        
  50.     }
  51.  
  52.     function update($post) {
  53.         $katNavn = $post['katNavn'];
  54.         $id = $post['ID'];
  55.         $query = "UPDATE " . $this->table_name . " SET katNavn = '". $katNavn ."' WHERE ID = '" .$id."'";
  56.  
  57.         $stmt = $this->conn->prepare($query);
  58.        
  59.         if(!$stmt) return $this->conn->errorInfo();
  60.  
  61.         return $stmt->execute();
  62.     }
  63.  
  64.     function delete($id) {
  65.         $query = "DELETE FROM " . $this->table_name . " WHERE ID = ".$id;
  66.  
  67.         $stmt = $this->conn->prepare($query);
  68.  
  69.         return $stmt->execute();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement