Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Joke Category management</title>
- </head>
- <body>
- <h1>Joke Category management</h1>
- <?php
- $dbcon = @mysql_connect(not for public consumption);
- if (!$dbcon) {
- exit('<p>Unable to connect to database server at this time.</p>');
- }
- if (!@mysql_select_db('ijdb')) {
- exit('<p>Unable to connect to database at this time.</p>');
- }
- if (isset($_GET['addCat'])):
- ?>
- <h2>Add new category:</h2>
- <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
- <p><label><input type="text" name="catName" /></label></p>
- <p><input type="submit" value="Add category" /></p>
- </form>
- <p><a href="/db-sites/ch6/cats.php">Return to category home</a></p>
- <?php elseif(!empty($_REQUEST['catName'])):
- $catName = $_REQUEST['catName'];
- if (isset($_REQUEST['catName'])) {
- $addCatSql = "insert into category set name='$catName'";
- if (@mysql_query($addCatSql)) {
- echo '<p>Added category to database.</p>';
- echo "<p><a href=". "/db-sites/ch6/cats.php " . ">Return to category home</a></p>";
- } else {
- exit('<p>Unable to add category' . mysql_error() . '</p>');
- }
- }
- ?>
- <?php elseif(!empty($_REQUEST['delCat'])):
- $delCat = $_REQUEST['delCat'];
- $delSql1 = @mysql_query("delete from jokecategory where categoryid='$delCat'");
- $delSql2 = @mysql_query("delete from category where id='$delCat'");
- if ($delSql1 and $delSql2) {
- echo "<p>Joke category has been deleted.";
- echo "<p><a href=". "/db-sites/ch6/cats.php " . ">Return to category home</a></p>";
- } else {
- exit('<p>Unable to delete category' . mysql_error() . '</p>');
- }
- ?>
- <?php elseif(!empty($_REQUEST['editCat'])):
- $catId = $_REQUEST['editCat'];
- $updCat = @mysql_query("select name from category where id='$catId'");
- if (!$updCat){
- exit('<p>Unable to run query at this time.' . mysql_error() . '</p>');
- }
- $updCat = mysql_fetch_array($updCat);
- $updCatName = $updCat['name'];
- $updCatName = htmlspecialchars($updCatName);
- ?>
- <h2>Edit category:</h2>
- <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
- <p><label><input type="text" name="catName1" value="<?php echo $updCatName; ?>" /></label></p>
- <input type="hidden" name="catId" value="<?php echo $catId ?>" />
- <p><input type="submit" value="Edit category" /></p>
- </form>
- <p><a href="/db-sites/ch6/cats.php">Return to category home</a></p>
- <?php
- if (!empty($_REQUEST['catName1']) and !empty($_REQUEST['catId'])) {
- $updNm = $_REQUEST['catName1'];
- $updCat = @mysql_query("update category set name='$updNm' where id='$catId'");
- if ($updCat) {
- echo "<p>Joke category has been updated.";
- echo "<p><a href=". "/db-sites/ch6/cats.php" . "</p>";
- } else {
- exit('<p>Unable to run query at this time.' . mysql_error() . '</p>');
- }
- }
- ?>
- <?php else:
- $listCats = @mysql_query("select * from category");
- if (!$listCats) {
- exit('<p>Unable to query database at this time:' . mysql_error() . '</p>');
- }
- ?>
- <table>
- <tr>
- <td>Category name</td>
- <td>Edit</td>
- <td>Delete</td>
- </tr>
- <?php
- while($catData = mysql_fetch_array($listCats)) {
- echo "<tr><td>" . $catData['name'] . "</td>" .
- "<td>" . "<a href=" . $_SERVER['PHP_SELF'] . "?editCat=" . $catData['id'] . ">" . "Edit" . "</a>" . "</td>" .
- "<td>" . "<a href=" . $_SERVER['PHP_SELF'] . "?delCat=" . $catData['id'] . ">" . "Delete" . "</a>" . "</td></tr>";
- }
- ?>
- </table>
- <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?addCat=1'; ?>">Add a new category</a></p>
- <p><a href="/db-sites/ch6/">Return to home</a></p>
- <?php endif; ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement