Item.php 4.1 KB

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