Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class crud
- {
- private $db;
- function __construct($DB_con)
- {
- $this->db = $DB_con;
- }
- public function create($user, $pass, $email)
- {
- try {
- $stmt = $this->db->prepare("INSERT INTO users(username, password, email) VALUES(:user, :pass, :email)");
- $stmt->bindParam(":user", $user);
- $stmt->bindParam(":pass", $pass);
- $stmt->bindParam(":email", $email);
- $stmt->execute();
- return true;
- } catch(PDOException $e) {
- echo $e->getMessage();
- }
- }
- public function delete($id)
- {
- $stmt = $this->db->prepare("DELETE FROM users WHERE id = :id");
- $stmt->bindParam(":id", $id);
- $stmt->execute();
- return true;
- }
- public function getID($id)
- {
- $stmt = $this->db->prepare("SELECT * FROM users WHERE id=:id");
- $stmt->execute(array(":id" => $id));
- $editrow = $stmt->fetch(PDO::FETCH_ASSOC);
- return $editrow;
- }
- public function update($id, $user, $pass, $email)
- {
- try
- {
- $stmt = $this->db->prepare("UPDATE users SET username=:user, password=:pass, email=:email WHERE id=:id");
- $stmt->bindParam(":user", $user);
- $stmt->bindParam(":pass", $pass);
- $stmt->bindParam(":email", $email);
- $stmt->bindParam(":id", $id);
- $stmt->execute();
- return true;
- } catch(PDOException $e) {
- echo $e->getMessage();
- return false;
- }
- }
- public function viewData($query)
- {
- $stmt = $this->db->prepare($query);
- $stmt->execute();
- if($stmt->rowCount() > 0)
- {
- while($row = $stmt->fetch(PDO::FETCH_ASSOC))
- {
- ?>
- <tr>
- <td><?php print($row['id']); ?></td>
- <td><?php print($row['username']); ?></td>
- <td><?php print($row['password']); ?></td>
- <td><?php print($row['email']); ?></td>
- <td><a href="edit.php?id=<?php print($row['id']); ?>">Edit</a></td>
- <td><a href="delete.php?id=<?php print($row['id']); ?>">Delete</a></td>
- </tr>
- <?php
- }
- } else {
- echo "No results found!";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement