Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. class crud
  4. {
  5. private $db;
  6.  
  7. function __construct($DB_con)
  8. {
  9. $this->db = $DB_con;
  10. }
  11.  
  12. public function create($user, $pass, $email)
  13. {
  14. try {
  15. $stmt = $this->db->prepare("INSERT INTO users(username, password, email) VALUES(:user, :pass, :email)");
  16. $stmt->bindParam(":user", $user);
  17. $stmt->bindParam(":pass", $pass);
  18. $stmt->bindParam(":email", $email);
  19. $stmt->execute();
  20. return true;
  21. } catch(PDOException $e) {
  22. echo $e->getMessage();
  23. }
  24. }
  25.  
  26. public function delete($id)
  27. {
  28. $stmt = $this->db->prepare("DELETE FROM users WHERE id = :id");
  29. $stmt->bindParam(":id", $id);
  30. $stmt->execute();
  31. return true;
  32. }
  33.  
  34. public function getID($id)
  35. {
  36. $stmt = $this->db->prepare("SELECT * FROM users WHERE id=:id");
  37. $stmt->execute(array(":id" => $id));
  38. $editrow = $stmt->fetch(PDO::FETCH_ASSOC);
  39. return $editrow;
  40. }
  41.  
  42. public function update($id, $user, $pass, $email)
  43. {
  44. try
  45. {
  46. $stmt = $this->db->prepare("UPDATE users SET username=:user, password=:pass, email=:email WHERE id=:id");
  47. $stmt->bindParam(":user", $user);
  48. $stmt->bindParam(":pass", $pass);
  49. $stmt->bindParam(":email", $email);
  50. $stmt->bindParam(":id", $id);
  51. $stmt->execute();
  52. return true;
  53.  
  54. } catch(PDOException $e) {
  55. echo $e->getMessage();
  56. return false;
  57. }
  58. }
  59.  
  60. public function viewData($query)
  61. {
  62. $stmt = $this->db->prepare($query);
  63. $stmt->execute();
  64.  
  65. if($stmt->rowCount() > 0)
  66. {
  67. while($row = $stmt->fetch(PDO::FETCH_ASSOC))
  68. {
  69. ?>
  70. <tr>
  71. <td><?php print($row['id']); ?></td>
  72. <td><?php print($row['username']); ?></td>
  73. <td><?php print($row['password']); ?></td>
  74. <td><?php print($row['email']); ?></td>
  75. <td><a href="edit.php?id=<?php print($row['id']); ?>">Edit</a></td>
  76. <td><a href="delete.php?id=<?php print($row['id']); ?>">Delete</a></td>
  77. </tr>
  78. <?php
  79. }
  80. } else {
  81. echo "No results found!";
  82. }
  83. }
  84. }
  85.  
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement