Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.50 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>Joke Category management</title>
  4.     </head>
  5.     <body>
  6.         <h1>Joke Category management</h1>
  7.         <?php
  8.         $dbcon = @mysql_connect(not for public consumption);
  9.         if (!$dbcon) {
  10.             exit('<p>Unable to connect to database server at this time.</p>');    
  11.         }
  12.         if (!@mysql_select_db('ijdb')) {
  13.             exit('<p>Unable to connect to database at this time.</p>');        
  14.         }        
  15.             if (isset($_GET['addCat'])):
  16.         ?>
  17.         <h2>Add new category:</h2>
  18.         <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  19.             <p><label><input type="text" name="catName" /></label></p>
  20.             <p><input type="submit" value="Add category" /></p>
  21.         </form>
  22.         <p><a href="/db-sites/ch6/cats.php">Return to category home</a></p>
  23.         <?php elseif(!empty($_REQUEST['catName'])):
  24.         $catName = $_REQUEST['catName'];
  25.         if (isset($_REQUEST['catName'])) {
  26.             $addCatSql = "insert into category set name='$catName'";
  27.                 if (@mysql_query($addCatSql)) {
  28.                     echo '<p>Added category to database.</p>';
  29.                     echo "<p><a href=". "/db-sites/ch6/cats.php " . ">Return to category home</a></p>";
  30.                 } else {
  31.                     exit('<p>Unable to add category' . mysql_error() . '</p>');
  32.                 }
  33.         }
  34.         ?>
  35.         <?php elseif(!empty($_REQUEST['delCat'])):
  36.         $delCat = $_REQUEST['delCat'];
  37.         $delSql1 = @mysql_query("delete from jokecategory where categoryid='$delCat'");
  38.         $delSql2 = @mysql_query("delete from category where id='$delCat'");
  39.             if ($delSql1 and $delSql2) {
  40.                 echo "<p>Joke category has been deleted.";
  41.                 echo "<p><a href=". "/db-sites/ch6/cats.php " . ">Return to category home</a></p>";    
  42.             } else {
  43.                 exit('<p>Unable to delete category' . mysql_error() . '</p>');    
  44.             }
  45.         ?>
  46.         <?php elseif(!empty($_REQUEST['editCat'])):
  47.         $catId = $_REQUEST['editCat'];
  48.         $updCat = @mysql_query("select name from category where id='$catId'");
  49.         if (!$updCat){
  50.             exit('<p>Unable to run query at this time.' . mysql_error() . '</p>');                
  51.         }
  52.         $updCat = mysql_fetch_array($updCat);
  53.         $updCatName = $updCat['name'];
  54.         $updCatName = htmlspecialchars($updCatName);        
  55.         ?>
  56.         <h2>Edit category:</h2>
  57.         <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  58.             <p><label><input type="text" name="catName1" value="<?php echo $updCatName; ?>" /></label></p>
  59.             <input type="hidden" name="catId" value="<?php echo $catId ?>" />
  60.             <p><input type="submit" value="Edit category" /></p>
  61.         </form>
  62.         <p><a href="/db-sites/ch6/cats.php">Return to category home</a></p>
  63.         <?php
  64.         if (!empty($_REQUEST['catName1']) and !empty($_REQUEST['catId'])) {
  65.             $updNm = $_REQUEST['catName1'];
  66.             $updCat = @mysql_query("update category set name='$updNm' where id='$catId'");
  67.                 if ($updCat) {
  68.                     echo "<p>Joke category has been updated.";
  69.                     echo "<p><a href=". "/db-sites/ch6/cats.php" . "</p>";    
  70.                 } else {
  71.                     exit('<p>Unable to run query at this time.' . mysql_error() . '</p>');                    
  72.                 }            
  73.         }        
  74.         ?>
  75.         <?php else:
  76.         $listCats = @mysql_query("select * from category");
  77.         if (!$listCats) {
  78.             exit('<p>Unable to query database at this time:' . mysql_error() . '</p>');    
  79.         }
  80.         ?>
  81.         <table>
  82.             <tr>
  83.                 <td>Category name</td>
  84.                 <td>Edit</td>
  85.                 <td>Delete</td>
  86.             </tr>
  87.         <?php
  88.         while($catData = mysql_fetch_array($listCats)) {
  89.             echo "<tr><td>" . $catData['name'] . "</td>" .
  90.                  "<td>" . "<a href=" . $_SERVER['PHP_SELF'] . "?editCat=" . $catData['id'] . ">" . "Edit" . "</a>" . "</td>" .
  91.                  "<td>" . "<a href=" . $_SERVER['PHP_SELF'] . "?delCat=" . $catData['id'] . ">" . "Delete" . "</a>" . "</td></tr>";    
  92.         }
  93.         ?>
  94.         </table>
  95.             <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?addCat=1'; ?>">Add a new category</a></p>
  96.             <p><a href="/db-sites/ch6/">Return to home</a></p>
  97.         <?php endif; ?>
  98.     </body>
  99. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement