Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once('connection.php');
- class User{
- /* const ERROR_USERNAME_TAKEN= 1;
- const ERROR_GENERAL= 2;
- const ERROR_OK= 0;*/
- private $db;
- private $userName;
- private $password;
- private $book_id;
- private $isAuthenticated;
- private $signUp;
- private $deleteBooks;
- // private $validationError;
- public function __construct()
- {
- $this->db = (new connection())->dbConnect();
- }
- public function setUserName($username)
- {
- $this->userName= $username;
- }
- public function setPassword($password)
- {
- $this->password= $password;
- }
- public function setBookId($book_id)
- {
- $this->book_id= $book_id;
- }
- public function getUserName()
- {
- return $this->username;
- }
- public function getPassword()
- {
- return $this->password;
- }
- public function getBookId()
- {
- return $this->book_id;
- }
- /* public function getErrorInfo()
- {
- $errorDescription= "";
- switch($this->validationError){
- case self::ERROR_USERNAME_TAKEN:
- $errorDescription= "YOur username is already taken";
- break;
- default:
- $errorDescription= "UNKNOWN ERROR";
- break;
- }
- return $info= array("code" => $this->validationError, "message" => $errorDescription );
- }*/
- //-------------------------------------------------------------------
- public function isAuthenticated()
- {
- $query = "SELECT 1 FROM loginUser WHERE username = :username AND password = :password";
- $statment = $this->db->prepare($query);
- $statment->execute(
- array(
- ':username' => $this->username,
- ':password' => $this->Password
- ));
- $result = $statment->fetchColumn();
- $this->isAuthenticated= ($result == "1");
- return $this->isAuthenticated;
- }
- //-------------------------------------------------------------------
- public function getBooks($start = 0, $limit = 2)
- {
- $sql_start = $start * $limit;
- $sql_limit = $limit;
- $query = "SELECT Library.nameOfBook, userBook.book_id, userBook.user_id FROM loginUser JOIN userBook JOIN Library ON userBook.user_id = loginUser.id AND userBook.book_id = Library.id WHERE loginUser.username=:username ORDER BY Library.nameOfBook ASC LIMIT $sql_start, $sql_limit";
- $statment = $this->db->prepare($query);
- $statment->execute([
- ':username' => $this->username
- ]);
- $result = $statment->fetchAll();
- echo "<table id='myTable' border='1'>
- <tr>
- <th><a onclick='sortTable(1)' href='#'>Books</a></th>
- <th>Action</th>
- </tr>";
- foreach($result as $row){
- echo "<tr>";
- echo "<td>" . $row['nameOfBook'] . "</td>";
- echo "<td>" ."<input type='submit' id='delete".$row['book_id']."-".$row['user_id']."' onclick='deleteBook(this)' name='delete' value='Delete'>" . "</td>";
- echo "</tr>";
- }
- echo "</table>";
- echo "";
- }
- //-------------------------------------------------------------------
- public function deleteBook($book_id, $user_id)
- {
- //echo "book_id is " . $book_id . ", user_id is " . $user_id;
- $stmt = $this->db->prepare("DELETE FROM userBook WHERE book_id = :book_id AND user_id = :user_id");
- if(!$stmt) echo $this->db->errorInfo();
- $stmt->bindValue(":book_id", $book_id);
- $stmt->bindValue(":user_id", $user_id);
- return $stmt->execute();
- }
- //-------------------------------------------------------------------
- public function signUp(&$errorInfo)
- {
- $statment = $this->db->prepare("SELECT COUNT(*) AS count FROM `loginUser` WHERE username=:username");
- $statment->execute(array(':username' => $this->username));
- if ($row = $statment->fetch(PDO::FETCH_ASSOC)) {
- $username_count = $row["count"];
- }
- if ($username_count > 0) {
- /* $this->validationError= self::ERROR_USERNAME_TAKEN;
- return false;*/
- echo "This user already taken";
- }
- else{ $statment = $this->db->prepare("INSERT INTO loginUser (username, password) VALUES(:username, :password)");
- $statment->bindParam(':username',$this->username);
- $statment->bindParam(':password',$this->Password);
- $statment->execute();
- $result = $statment->rowCount();
- $this->signUp= ($result == "1");
- return $this->signUp;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement