Advertisement
UME14

DB_CRUD_E-Book_Organizer

Sep 27th, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.55 KB | None | 0 0
  1. <?php
  2. class MyClass
  3. {
  4.     /*
  5.         Database Class guide:
  6.         Sorted by CRUD order. See schema below -
  7.         C - Insert
  8.         R - Show
  9.         U - Update
  10.         D - Delete
  11.     */
  12.    
  13.     public $db;
  14.     function __construct($db = "ebook1")
  15.     {
  16.         $this->db = new mysqli("localhost", "root", "", "$db");
  17.     }
  18.  
  19.     // CATEGORY
  20.     public function insertCategory($name)
  21.     {
  22.         $m = $this->db;
  23.         $q = "INSERT INTO categories SET cat_name='$name'";
  24.         $r = $m->query($q);
  25.  
  26.         if (!$r) {
  27.             echo "An error occured while trying to enter new CATEGORY into database. <br/>";
  28.         }
  29.     }
  30.  
  31.     /* Table: AUTHORS */
  32.     public function insertAuthor($name, $address, $email, $contacts, $fb)
  33.     {
  34.         $m = $this->db;
  35.         $q = "INSERT INTO authors SET author_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb'";
  36.         $r = $m->query($q);
  37.  
  38.         if (!$r) {
  39.             echo "An error occured while trying to enter new Author into database. <br/>";
  40.         }
  41.     }
  42.  
  43.     public function showAuthor_single($id)
  44.     {
  45.         $m = $this->db;
  46.         $q = "SELECT * FROM authors WHERE author_id='$id'";
  47.         $r = $m->query($q);
  48.  
  49.         if (!$r) {
  50.             echo "An error occured while trying to show 1 Author from database. <br/>";
  51.             return;
  52.         }
  53.  
  54.         return $r;
  55.     }
  56.  
  57.     public function showAuthor_all()
  58.     {
  59.         $m = $this->db;
  60.         $q = "SELECT * FROM authors";
  61.         $r = $m->query($q);
  62.  
  63.         if (!$r) {
  64.             echo "An error occured while trying to show 1 Author from database. <br/>";
  65.             return;
  66.         }
  67.  
  68.         return $r;
  69.     }
  70.  
  71.     public function updateAuthor($id, $name, $address, $email, $contacts, $fb)
  72.     {
  73.         $m = $this->db;
  74.         $q = "UPDATE authors SET author_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb' WHERE author_id='$id'";
  75.         $r = $m->query($q);
  76.  
  77.         if (!$r) {
  78.             echo "An error occured while trying to update 1 Author from database. <br/>";
  79.         }
  80.     }
  81.  
  82.     public function deleteAuthor($id)
  83.     {
  84.         $m = $this->db;
  85.         $q = "DELETE * FROM authors WHERE author_id='$id'";
  86.         $r = $m->query($q);
  87.  
  88.         if (!$r) {
  89.             echo "An error occured while trying to delete 1 Author from database. <br/>";
  90.         }
  91.     }
  92.  
  93.     /* TABLE: publisher */
  94.     public function insertPublisher($name, $address, $email, $contacts, $fb)
  95.     {
  96.         $m = $this->db;
  97.         $q = "INSERT INTO publisher SET pub_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb'";
  98.         $r = $m->query($q);
  99.  
  100.         if (!$r) {
  101.             echo "An error occured while trying to enter new Publisher into database. <br/>";
  102.         }
  103.     }
  104.  
  105.     public function showPublisher_single($id)
  106.     {
  107.         $m = $this->db;
  108.         $q = "SELECT * FROM publisher WHERE pub_id='$id'";
  109.         $r = $m->query($q);
  110.  
  111.         if (!$r) {
  112.             echo "An error occured while trying to show 1 Publisher from database. <br/>";
  113.             return;
  114.         }
  115.  
  116.         return $r;
  117.     }
  118.  
  119.     public function showPublisher_all()
  120.     {
  121.         $m = $this->db;
  122.         $q = "SELECT * FROM publisher";
  123.         $r = $m->query($q);
  124.  
  125.         if (!$r) {
  126.             echo "An error occured while trying to show 1 Publisher from database. <br/>";
  127.             return;
  128.         }
  129.  
  130.         return $r;
  131.     }
  132.  
  133.     public function updatePublisher($name, $address, $email, $contacts, $fb)
  134.     {
  135.         $m = $this->db;
  136.         $q = "UPDATE publisher SET pub_name='$name', address='$address', email='$email', contacts='$contacts', fb='$fb'";
  137.         $r = $m->query($q);
  138.  
  139.         if (!$r) {
  140.             echo "An error occured while trying to update 1 Publisher from database. <br/>";
  141.         }
  142.     }
  143.  
  144.     public function deletePublisher($id)
  145.     {
  146.         $m = $this->db;
  147.         $q = "DELETE * FROM publisher WHERE pub_id='$id'";
  148.         $r = $m->query($q);
  149.  
  150.         if (!$r) {
  151.             echo "An error occured while trying to delete 1 Publisher from database. <br/>";
  152.         }
  153.     }
  154.  
  155.     /* TABLE: items */
  156.     public function insertItem($name, $cat_id, $description, $rating, $release)
  157.     {
  158.         $m = $this->db;
  159.         $q = "INSERT INTO items SET item_name='$name', cat_id='$cat_id', description='$description', rating='$rating', release_yr='$release'";
  160.         $r = $m->query($q);
  161.  
  162.         if (!$r) {
  163.             echo "An error occured while trying to enter new Item into database. <br/>";
  164.         }
  165.     }
  166.  
  167.     public function showItem_single($id)
  168.     {
  169.         $m = $this->db;
  170.         $q = "SELECT * FROM items WHERE item_id='$id'";
  171.         $r = $m->query($q);
  172.  
  173.         if (!$r) {
  174.             echo "An error occured while trying to show 1 Item from database. <br/>";
  175.             return;
  176.         }
  177.  
  178.         return $r;
  179.     }
  180.  
  181.     public function showItems_all()
  182.     {
  183.         $m = $this->db;
  184.         $q = "SELECT * FROM items";
  185.         $r = $m->query($q);
  186.  
  187.         if (!$r) {
  188.             echo "An error occured while trying to show items from database. <br/>";
  189.             return;
  190.         }
  191.  
  192.         return $r;
  193.     }
  194.  
  195.     public function updateItem($id, $name, $cat_id, $description, $rating, $release)
  196.     {
  197.         $m = $this->db;
  198.         $q = "UPDATE items SET item_name='$name', cat_id='$cat_id', description='$description', rating='$rating', release='$release' WHERE item_id='$id'";
  199.         $r = $m->query($q);
  200.  
  201.         if (!$r) {
  202.             echo "An error occured while trying to delete 1 item from database. <br/>";
  203.         }
  204.     }
  205.  
  206.     public function deleteItem($id)
  207.     {
  208.         $m = $this->db;
  209.         $q = "DELETE * FROM items WHERE item_id='$id'";
  210.         $r = $m->query($q);
  211.  
  212.         if (!$r) {
  213.             echo "An error occured while trying to delete 1 Author from database. <br/>";
  214.         }
  215.     }
  216.  
  217.     /* TABLE: items_authors */ /* ===================== PROBLEM WITH THIS TABLE ========================================= */
  218.     public function insertItemAuthors($slno, $item_id, $auth_id, $item_sl)
  219.     {
  220.         $m = $this->db;
  221.         $q = "INSERT INTO items_authors SET slno='$slno', item_id='$item_id', auth_id='$auth_id', item_sl='$item_sl'";
  222.         $r = $m->query($q);
  223.  
  224.         if (!$r) {
  225.             echo "An error occured while trying to enter new Item into database. <br/>";
  226.         }
  227.     }
  228.  
  229.     public function showItemAuthor_single($id)
  230.     {
  231.         $m = $this->db;
  232.         $q = "SELECT * FROM items_authors WHERE item_id='$id'";
  233.         $r = $m->query($q);
  234.  
  235.         if (!$r) {
  236.             echo "An error occured while trying to show 1 Item from database. <br/>";
  237.             return;
  238.         }
  239.  
  240.         return $r;
  241.     }
  242.  
  243.     public function showItemAuthors_all()
  244.     {
  245.         $m = $this->db;
  246.         $q = "SELECT * FROM items_authors";
  247.         $r = $m->query($q);
  248.  
  249.         if (!$r) {
  250.             echo "An error occured while trying to show items from database. <br/>";
  251.             return;
  252.         }
  253.  
  254.         return $r;
  255.     }
  256.  
  257.     public function updateItemAuthors($slno, $item_id, $auth_id, $item_sl)
  258.     {
  259.         $m = $this->db;
  260.         $q = "UPDATE authors SET slno='$slno', item_id='$item_id', auth_id='$auth_id', item_sl='$item_sl' WHERE slno='$slno'";
  261.         $r = $m->query($q);
  262.  
  263.         if (!$r) {
  264.             echo "An error occured while trying to update 1 item from database. <br/>";
  265.         }
  266.     }
  267.  
  268.     public function deleteItemAuthors($slno)
  269.     {
  270.         $m = $this->db;
  271.         $q = "DELETE * FROM authors WHERE slno='$slno'";
  272.         $r = $m->query($q);
  273.  
  274.         if (!$r) {
  275.             echo "An error occured while trying to delete 1 Author from database. <br/>";
  276.         }
  277.     }
  278.    
  279.     /* TABLE: item_publishers */
  280.     public function insertItemPublisher($slno, $item_id, $pub_id, $item_sl)
  281.     {
  282.         $m = $this->db;
  283.         $q = "INSERT INTO item_publishers SET slno='$slno', item_id='$item_id', pub_id='$pub_id', item_sl='$item_sl'";
  284.         $r = $m->query($q);
  285.  
  286.         if (!$r) {
  287.             echo "An error occured while trying to enter new Item Publisher into database. <br/>";
  288.         }
  289.     }
  290.  
  291.     public function showItemPublisher_single($slno)
  292.     {
  293.         $m = $this->db;
  294.         $q = "SELECT * FROM item_publishers WHERE slno='$slno'";
  295.         $r = $m->query($q);
  296.  
  297.         if (!$r) {
  298.             echo "An error occured while trying to show 1 Item Publisher from database. <br/>";
  299.             return;
  300.         }
  301.  
  302.         return $r;
  303.     }
  304.  
  305.     public function showItemPublishers_all()
  306.     {
  307.         $m = $this->db;
  308.         $q = "SELECT * FROM item_publishers";
  309.         $r = $m->query($q);
  310.  
  311.         if (!$r) {
  312.             echo "An error occured while trying to show Item Publisher from database. <br/>";
  313.             return;
  314.         }
  315.  
  316.         return $r;
  317.     }
  318.  
  319.     public function updateItemPublishers($slno, $item_id, $pub_id, $item_sl)
  320.     {
  321.         $m = $this->db;
  322.         $q = "UPDATE item_publishers SET slno='$slno', item_id='$item_id', pub_id='$pub_id', item_sl='$item_sl'";
  323.         $r = $m->query($q);
  324.  
  325.         if (!$r) {
  326.             echo "An error occured while trying to update 1 Item Publisher in database. <br/>";
  327.         }
  328.     }
  329.  
  330.     public function deleteItemPublishers($slno)
  331.     {
  332.         $m = $this->db;
  333.         $q = "DELETE * FROM item_publishers WHERE slno='$slno'";
  334.         $r = $m->query($q);
  335.  
  336.         if (!$r) {
  337.             echo "An error occured while trying to delete 1 Author from database. <br/>";
  338.         }
  339.     }
  340.     // 1.5 hrs
  341. }
  342. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement