Guest User

Untitled

a guest
Feb 2nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?php
  2. include_once $_SERVER['DOCUMENT_ROOT'] .
  3.     '/includes/magicquotes.inc.php';
  4.  
  5. if (isset($_GET['add']))
  6. {
  7.   $pageTitle = 'New Author';
  8.   $action = 'addform';
  9.   $name = '';
  10.   $email = '';
  11.   $id = '';
  12.   $button = 'Add author';
  13.  
  14.   include 'form.html.php';
  15.   exit();
  16. }
  17.  
  18. if (isset($_GET['addform']))
  19. {
  20.   include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
  21.  
  22.   try
  23.   {
  24.     $sql = 'INSERT INTO author SET
  25.        name = :name,
  26.        email = :email';
  27.     $s = $pdo->prepare($sql);
  28.     $s->bindValue(':name', $_POST['name']);
  29.     $s->bindValue(':email', $_POST['email']);
  30.     $s->execute();
  31.   }
  32.   catch (PDOException $e)
  33.   {
  34.     $error = 'Error adding submitted author.';
  35.     include 'error.html.php';
  36.     exit();
  37.   }
  38.  
  39.   header('Location: .');
  40.   exit();
  41. }
  42.  
  43. if (isset($_POST['action']) and $_POST['action'] == 'Edit')
  44. {
  45.   include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
  46.  
  47.   try
  48.   {
  49.     $sql = 'SELECT id, name, email FROM author WHERE id = :id';
  50.     $s = $pdo->prepare($sql);
  51.     $s->bindValue(':id', $_POST['id']);
  52.     $s->execute();
  53.   }
  54.   catch (PDOException $e)
  55.   {
  56.     $error = 'Error fetching author details.';
  57.     include 'error.html.php';
  58.     exit();
  59.   }
  60.  
  61.   $row = $s->fetch();
  62.  
  63.   $pageTitle = 'Edit Author';
  64.   $action = 'editform';
  65.   $name = $row['name'];
  66.   $email = $row['email'];
  67.   $id = $row['id'];
  68.   $button = 'Update author';
  69.  
  70.   include 'form.html.php';
  71.   exit();
  72. }
  73.  
  74. if (isset($_GET['editform']))
  75. {
  76.   include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
  77.  
  78.   try
  79.   {
  80.     $sql = 'UPDATE author SET
  81.        name = :name,
  82.        email = :email
  83.        WHERE id = :id';
  84.     $s = $pdo->prepare($sql);
  85.     $s->bindValue(':id', $_POST['id']);
  86.     $s->bindValue(':name', $_POST['name']);
  87.     $s->bindValue(':email', $_POST['email']);
  88.     $s->execute();
  89.   }
  90.   catch (PDOException $e)
  91.   {
  92.     $error = 'Error updating submitted author.';
  93.     include 'error.html.php';
  94.     exit();
  95.   }
  96.  
  97.   header('Location: .');
  98.   exit();
  99. }
  100.  
  101. if (isset($_POST['action']) and $_POST['action'] == 'Delete')
  102. {
  103.   include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
  104.  
  105.   // Get jokes belonging to author
  106.   try
  107.   {
  108.     $sql = 'SELECT id FROM joke WHERE authorid = :id';
  109.     $s = $pdo->prepare($sql);
  110.     $s->bindValue(':id', $_POST['id']);
  111.     $s->execute();
  112.   }
  113.   catch (PDOException $e)
  114.   {
  115.     $error = 'Error getting list of jokes to delete.';
  116.     include 'error.html.php';
  117.     exit();
  118.   }
  119.  
  120.   $result = $s->fetchAll();
  121.  
  122.   // Delete joke category entries
  123.   try
  124.   {
  125.     $sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
  126.     $s = $pdo->prepare($sql);
  127.  
  128.     // For each joke
  129.     foreach ($result as $row)
  130.     {
  131.       $jokeId = $row['id'];
  132.       $s->bindValue(':id', $jokeId);
  133.       $s->execute();
  134.     }
  135.   }
  136.   catch (PDOException $e)
  137.   {
  138.     $error = 'Error deleting category entries for joke.';
  139.     include 'error.html.php';
  140.     exit();
  141.   }
  142.  
  143.   // Delete jokes belonging to author
  144.   try
  145.   {
  146.     $sql = 'DELETE FROM joke WHERE authorid = :id';
  147.     $s = $pdo->prepare($sql);
  148.     $s->bindValue(':id', $_POST['id']);
  149.     $s->execute();
  150.   }
  151.   catch (PDOException $e)
  152.   {
  153.     $error = 'Error deleting jokes for author.';
  154.     include 'error.html.php';
  155.     exit();
  156.   }
  157.  
  158.   // Delete the author
  159.   try
  160.   {
  161.     $sql = 'DELETE FROM author WHERE id = :id';
  162.     $s = $pdo->prepare($sql);
  163.     $s->bindValue(':id', $_POST['id']);
  164.     $s->execute();
  165.   }
  166.   catch (PDOException $e)
  167.   {
  168.     $error = 'Error deleting author.';
  169.     include 'error.html.php';
  170.     exit();
  171.   }
  172.  
  173.   header('Location: .');
  174.   exit();
  175. }
  176.  
  177. // Display author list
  178. include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
  179.  
  180. try
  181. {
  182.   $result = $pdo->query('SELECT id, name FROM author');
  183. }
  184. catch (PDOException $e)
  185. {
  186.   $error = 'Error fetching authors from the database!';
  187.   include 'error.html.php';
  188.   exit();
  189. }
  190.  
  191. foreach ($result as $row)
  192. {
  193.   $authors[] = array('id' => $row['id'], 'name' => $row['name']);
  194. }
  195.  
  196. include 'authors.html.php';
Add Comment
Please, Sign In to add comment