Guest User

Untitled

a guest
Feb 21st, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: iNs8T
  5. * Date: 06/02/16
  6. * Time: 00:39
  7. */
  8.  
  9. class CRUD_Model
  10. {
  11.  
  12. private $pdocon;
  13. private $dsn, $username, $password;
  14. private $link;
  15.  
  16. function __construct()
  17. {
  18. /*$this->dsn = 'mysql:host=127.0.0.1:3306;dbname=products';
  19. $this->username = 'root';
  20. $this->password = '';*/
  21.  
  22. $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
  23. $this->username = 'kayjays_se';
  24. $this->password = 'hej123';
  25. }
  26.  
  27. private function openConnection()
  28. {
  29. try {
  30. if ($this->pdocon == NULL) {
  31. $this->pdocon = new PDO($this->dsn, $this->username, $this->password);
  32. }
  33. }catch (PDOException $pdoexp) {
  34. $this->pdocon = NULL;
  35. throw new Exception('Database error');
  36. }
  37. }
  38.  
  39.  
  40. /* public function addItem() {
  41. try {
  42. $this->openConnection();
  43.  
  44. $pdoStatement = $pdocon->prepare('INSERT INTO products (description, price, added_At, color, size, imagepath, gender, category, quantity, name) VALUES(:description, :price, :added_At, :color, :size, :imagepath, :gender, :category, :quantity, :name)');
  45.  
  46.  
  47.  
  48. $pdoStatement->bindParam(':description', $_POST['description']);
  49. $pdoStatement->bindParam(':price', $_POST['price']);
  50. $pdoStatement->bindParam(':added_At', $_POST['added_At']);
  51. $pdoStatement->bindParam(':color', $_POST['color']);
  52. $pdoStatement->bindParam(':size', $_POST['size']);
  53. $pdoStatement->bindParam(':imagepath', $_POST['imagepath']);
  54. $pdoStatement->bindParam(':gender', $_POST['gender']);
  55. $pdoStatement->bindParam(':category', $_POST['category']);
  56. $pdoStatement->bindParam(':quantity', $_POST['quantity']);
  57. $pdoStatement->bindParam(':name', $_POST['name']);
  58.  
  59. $pdoStatement->execute();
  60.  
  61. $this->pdocon = NULL;
  62. } catch (PDOException $pdoexp) {
  63. $this->pdocon = NULL;
  64. throw new Exception('Database error');
  65. }
  66. } */
  67.  
  68. public function addItem() {
  69. try {
  70. $this->dsn = 'mysql:host=kayjays.se.mysql;dbname=kayjays_se';
  71. $this->username = 'kayjays_se';
  72. $this->password = 'hej123';
  73.  
  74. //this->openConnection();
  75. $pdocon = new PDO($this->dsn, $this->username, $this->password);
  76.  
  77. //Preparar ett statement som kommer lägga till i tabellen products
  78. $pdoStatement = $pdocon->prepare('INSERT INTO products (name, category,
  79. imagepath, size, price, gender) VALUES(:name, :category, :imagepath, :size, :price, :gender)');
  80. //binder ihop $_Post värdena med en parameter
  81. $pdoStatement->bindParam(':name', $_POST['name']);
  82. $pdoStatement->bindParam(':category' ,$_POST['category']);
  83. $pdoStatement->bindParam(':imagepath', $_POST['imagepath']);
  84. $pdoStatement->bindParam('size', $_POST['size']);
  85. $pdoStatement->bindParam(':price', $_POST['price']);
  86. $pdoStatement->bindParam(':gender', $_POST['gender']);
  87.  
  88.  
  89. //kör vårt tidigare angivna statement
  90. $pdoStatement->execute();
  91.  
  92.  
  93.  
  94.  
  95.  
  96. $this->pdocon = NULL;
  97. } catch (PDOException $pdoexp) {
  98. $this->pdocon = NULL;
  99. throw new Exception('Database error');
  100. }
  101. }
  102.  
  103. //Funktion för att radera ett item ur databasen
  104. public function deleteItem($id){
  105. try{
  106. $this->openConnection();
  107. //preparerar ett sqlstatement
  108. $pdoStatement = $this->pdocon->prepare('DELETE FROM products WHERE id=:id');
  109.  
  110. $pdoStatement->bindParam(':id', $id);
  111. //kör vårt tidigare angivna statement
  112. $pdoStatement->execute();
  113.  
  114. $this->pdocon = NULL;
  115.  
  116. } catch (Exception $ex) {
  117. $this->pdocon = NULL;
  118. throw new Exception('Database error');
  119.  
  120. }
  121. }
  122. //funktion för att uppdatera ett item i shopen
  123. public function updateItem() {
  124. try {
  125. $this->openConnection();
  126. //updaterar produkten i databasen där vi hittar matchning på ID
  127. $pdoStatement = $this->pdocon->prepare('UPDATE `products` SET `description`=:description,`price`=:price,`added_At`=:added_At,`color`=:color,`size`=:size,`imagepath`=:imagepath,`gender`=:gender,`category`=:category,`quantity`=:quantity,`name`=:name WHERE `id`=:id');
  128. //binder ihop datat från de olika $_post
  129. $pdoStatement->bindParam(':description', $_POST['description']);
  130. $pdoStatement->bindParam(':price', $_POST['price']);
  131. $pdoStatement->bindParam(':added_At', $_POST['added_At']);
  132. $pdoStatement->bindParam(':color', $_POST['color']);
  133. $pdoStatement->bindParam(':size', $_POST['size']);
  134. $pdoStatement->bindParam(':imagepath', $_POST['imagepath']);
  135. $pdoStatement->bindParam(':gender', $_POST['gender']);
  136. $pdoStatement->bindParam(':category', $_POST['category']);
  137. $pdoStatement->bindParam(':quantity', $_POST['quantity']);
  138. $pdoStatement->bindParam(':name', $_POST['name']);
  139. $pdoStatement->bindParam(':id', $_POST['id']);
  140.  
  141. //kör vårt tidigare angivna statement
  142. $pdoStatement->execute();
  143.  
  144. $this->pdocon = NULL;
  145. } catch (PDOException $pdoexp) {
  146. $this->pdocon = NULL;
  147. throw new Exception('Databaseerror');
  148. }
  149. }
  150.  
  151.  
  152. }
  153. ?>
Add Comment
Please, Sign In to add comment