Item.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. class Item {
  3. private $conn;
  4. private $table_name = "items";
  5. public $id;
  6. public $name;
  7. public $description;
  8. public $serial_number;
  9. public $picture;
  10. public $quantity;
  11. public $price;
  12. public $date_of_purchase;
  13. public $created_at;
  14. public $updated_at;
  15. public function __construct($db) {
  16. $this->conn = $db;
  17. }
  18. public function create() {
  19. $query = "INSERT INTO " . $this->table_name . " SET name=:name, description=:description, serial_number=:serial_number, picture=:picture, quantity=:quantity, price=:price, date_of_purchase=:date_of_purchase, created_at=:created_at, updated_at=:updated_at";
  20. $stmt = $this->conn->prepare($query);
  21. $this->name = htmlspecialchars(strip_tags($this->name));
  22. $this->description = htmlspecialchars(strip_tags($this->description));
  23. $this->serial_number = htmlspecialchars(strip_tags($this->serial_number));
  24. $this->picture = htmlspecialchars(strip_tags($this->picture));
  25. $this->quantity = htmlspecialchars(strip_tags($this->quantity));
  26. $this->price = htmlspecialchars(strip_tags($this->price));
  27. $this->date_of_purchase = htmlspecialchars(strip_tags($this->date_of_purchase));
  28. $this->created_at = date('Y-m-d H:i:s');
  29. $this->updated_at = date('Y-m-d H:i:s');
  30. $stmt->bindParam(":name", $this->name);
  31. $stmt->bindParam(":description", $this->description);
  32. $stmt->bindParam(":serial_number", $this->serial_number);
  33. $stmt->bindParam(":picture", $this->picture);
  34. $stmt->bindParam(":quantity", $this->quantity);
  35. $stmt->bindParam(":price", $this->price);
  36. $stmt->bindParam(":date_of_purchase", $this->date_of_purchase);
  37. $stmt->bindParam(":created_at", $this->created_at);
  38. $stmt->bindParam(":updated_at", $this->updated_at);
  39. if($stmt->execute()) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. public function read() {
  45. $query = "SELECT * FROM " . $this->table_name . " ORDER BY created_at DESC";
  46. $stmt = $this->conn->prepare($query);
  47. $stmt->execute();
  48. return $stmt;
  49. }
  50. public function readOne() {
  51. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  52. $stmt = $this->conn->prepare($query);
  53. $stmt->bindParam(1, $this->id);
  54. $stmt->execute();
  55. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  56. $this->name = $row['name'];
  57. $this->description = $row['description'];
  58. $this->serial_number = $row['serial_number'];
  59. $this->picture = $row['picture'];
  60. $this->quantity = $row['quantity'];
  61. $this->price = $row['price'];
  62. $this->date_of_purchase = $row['date_of_purchase'];
  63. $this->created_at = $row['created_at'];
  64. $this->updated_at = $row['updated_at'];
  65. }
  66. public function update() {
  67. $query = "UPDATE " . $this->table_name . " SET name=:name, description=:description, serial_number=:serial_number, picture=:picture, quantity=:quantity, price=:price, date_of_purchase=:date_of_purchase, updated_at=:updated_at WHERE id=:id";
  68. $stmt = $this->conn->prepare($query);
  69. $this->name = htmlspecialchars(strip_tags($this->name));
  70. $this->description = htmlspecialchars(strip_tags($this->description));
  71. $this->serial_number = htmlspecialchars(strip_tags($this->serial_number));
  72. $this->picture = htmlspecialchars(strip_tags($this->picture));
  73. $this->quantity = htmlspecialchars(strip_tags($this->quantity));
  74. $this->price = htmlspecialchars(strip_tags($this->price));
  75. $this->date_of_purchase = htmlspecialchars(strip_tags($this->date_of_purchase));
  76. $this->updated_at = date('Y-m-d H:i:s');
  77. $stmt->bindParam(":name", $this->name);
  78. $stmt->bindParam(":description", $this->description);
  79. $stmt->bindParam(":serial_number", $this->serial_number);
  80. $stmt->bindParam(":picture", $this->picture);
  81. $stmt->bindParam(":quantity", $this->quantity);
  82. $stmt->bindParam(":price", $this->price);
  83. $stmt->bindParam(":date_of_purchase", $this->date_of_purchase);
  84. $stmt->bindParam(":updated_at", $this->updated_at);
  85. $stmt->bindParam(":id", $this->id);
  86. if($stmt->execute()) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. public function delete() {
  92. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  93. $stmt = $this->conn->prepare($query);
  94. $stmt->bindParam(1, $this->id);
  95. if($stmt->execute()) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. }
  101. ?>