Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MyClass
- {
- /*
- Database Class guide:
- Sorted by CRUD order. See schema below -
- C - Insert
- R - Show
- U - Update
- D - Delete
- */
- public $db;
- function __construct($db = "ebook1")
- {
- $this->db = new mysqli("localhost", "root", "", "$db");
- }
- // CATEGORY
- public function insertCategory($name)
- {
- $m = $this->db;
- $q = "INSERT INTO categories SET cat_name='$name'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to enter new CATEGORY into database. <br/>";
- }
- }
- /* Table: AUTHORS */
- public function insertAuthor($name, $address, $email, $contacts, $fb)
- {
- $m = $this->db;
- $q = "INSERT INTO authors SET author_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to enter new Author into database. <br/>";
- }
- }
- public function showAuthor_single($id)
- {
- $m = $this->db;
- $q = "SELECT * FROM authors WHERE author_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Author from database. <br/>";
- return;
- }
- return $r;
- }
- public function showAuthor_all()
- {
- $m = $this->db;
- $q = "SELECT * FROM authors";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Author from database. <br/>";
- return;
- }
- return $r;
- }
- public function updateAuthor($id, $name, $address, $email, $contacts, $fb)
- {
- $m = $this->db;
- $q = "UPDATE authors SET author_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb' WHERE author_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to update 1 Author from database. <br/>";
- }
- }
- public function deleteAuthor($id)
- {
- $m = $this->db;
- $q = "DELETE * FROM authors WHERE author_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to delete 1 Author from database. <br/>";
- }
- }
- /* TABLE: publisher */
- public function insertPublisher($name, $address, $email, $contacts, $fb)
- {
- $m = $this->db;
- $q = "INSERT INTO publisher SET pub_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to enter new Publisher into database. <br/>";
- }
- }
- public function showPublisher_single($id)
- {
- $m = $this->db;
- $q = "SELECT * FROM publisher WHERE pub_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Publisher from database. <br/>";
- return;
- }
- return $r;
- }
- public function showPublisher_all()
- {
- $m = $this->db;
- $q = "SELECT * FROM publisher";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Publisher from database. <br/>";
- return;
- }
- return $r;
- }
- public function updatePublisher($name, $address, $email, $contacts, $fb)
- {
- $m = $this->db;
- $q = "UPDATE publisher SET pub_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to update 1 Publisher from database. <br/>";
- }
- }
- public function deletePublisher($id)
- {
- $m = $this->db;
- $q = "DELETE * FROM publisher WHERE pub_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to delete 1 Publisher from database. <br/>";
- }
- }
- /* TABLE: items */
- public function insertItem($name, $cat_id, $description, $rating, $release)
- {
- $m = $this->db;
- $q = "INSERT INTO items SET item_name='$name', cat_id='$cat_id', description='$description', rating='$rating', release_yr='$release'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to enter new Item into database. <br/>";
- }
- }
- public function showItem_single($id)
- {
- $m = $this->db;
- $q = "SELECT * FROM items WHERE item_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Item from database. <br/>";
- return;
- }
- return $r;
- }
- public function showItems_all()
- {
- $m = $this->db;
- $q = "SELECT * FROM items";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show items from database. <br/>";
- return;
- }
- return $r;
- }
- public function updateItem($id, $name, $cat_id, $description, $rating, $release)
- {
- $m = $this->db;
- $q = "UPDATE items SET item_name='$name', cat_id='$cat_id', description='$description', rating='$rating', release='$release' WHERE item_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to delete 1 item from database. <br/>";
- }
- }
- public function deleteItem($id)
- {
- $m = $this->db;
- $q = "DELETE * FROM items WHERE item_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to delete 1 Author from database. <br/>";
- }
- }
- /* TABLE: items_authors */ /* ===================== PROBLEM WITH THIS TABLE ========================================= */
- public function insertItemAuthors($slno, $item_id, $auth_id, $item_sl)
- {
- $m = $this->db;
- $q = "INSERT INTO items_authors SET slno='$slno', item_id='$item_id', auth_id='$auth_id', item_sl='$item_sl'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to enter new Item into database. <br/>";
- }
- }
- public function showItemAuthor_single($id)
- {
- $m = $this->db;
- $q = "SELECT * FROM items_authors WHERE item_id='$id'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Item from database. <br/>";
- return;
- }
- return $r;
- }
- public function showItemAuthors_all()
- {
- $m = $this->db;
- $q = "SELECT * FROM items_authors";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show items from database. <br/>";
- return;
- }
- return $r;
- }
- public function updateItemAuthors($slno, $item_id, $auth_id, $item_sl)
- {
- $m = $this->db;
- $q = "UPDATE authors SET slno='$slno', item_id='$item_id', auth_id='$auth_id', item_sl='$item_sl' WHERE slno='$slno'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to update 1 item from database. <br/>";
- }
- }
- public function deleteItemAuthors($slno)
- {
- $m = $this->db;
- $q = "DELETE * FROM authors WHERE slno='$slno'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to delete 1 Author from database. <br/>";
- }
- }
- /* TABLE: item_publishers */
- public function insertItemPublisher($slno, $item_id, $pub_id, $item_sl)
- {
- $m = $this->db;
- $q = "INSERT INTO item_publishers SET slno='$slno', item_id='$item_id', pub_id='$pub_id', item_sl='$item_sl'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to enter new Item Publisher into database. <br/>";
- }
- }
- public function showItemPublisher_single($slno)
- {
- $m = $this->db;
- $q = "SELECT * FROM item_publishers WHERE slno='$slno'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show 1 Item Publisher from database. <br/>";
- return;
- }
- return $r;
- }
- public function showItemPublishers_all()
- {
- $m = $this->db;
- $q = "SELECT * FROM item_publishers";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to show Item Publisher from database. <br/>";
- return;
- }
- return $r;
- }
- public function updateItemPublishers($slno, $item_id, $pub_id, $item_sl)
- {
- $m = $this->db;
- $q = "UPDATE item_publishers SET slno='$slno', item_id='$item_id', pub_id='$pub_id', item_sl='$item_sl'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to update 1 Item Publisher in database. <br/>";
- }
- }
- public function deleteItemPublishers($slno)
- {
- $m = $this->db;
- $q = "DELETE * FROM item_publishers WHERE slno='$slno'";
- $r = $m->query($q);
- if (!$r) {
- echo "An error occured while trying to delete 1 Author from database. <br/>";
- }
- }
- // 1.5 hrs
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement