/admin/blog_update_mysqli.php

https://github.com/lisawilliams/phpsols · PHP · 167 lines · 142 code · 6 blank · 19 comment · 25 complexity · 171955ba6fe840bc614d3cbb0c335ebb MD5 · raw file

  1. <?php
  2. if (get_magic_quotes_gpc() === 1)
  3. {
  4. $_GET = json_decode(stripslashes(json_encode($_GET, JSON_HEX_APOS)), true);
  5. $_POST = json_decode(stripslashes(json_encode($_POST, JSON_HEX_APOS)), true);
  6. $_COOKIE = json_decode(stripslashes(json_encode($_COOKIE, JSON_HEX_APOS)), true);
  7. $_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true);
  8. }
  9. include('../includes/connection.inc.php');
  10. // initialize flags
  11. $OK = false;
  12. $done = false;
  13. // create database connection
  14. $conn = dbConnect('write');
  15. // initialize statement
  16. $stmt = $conn->stmt_init();
  17. // get details of selected record
  18. if (isset($_GET['article_id']) && !$_POST) {
  19. // prepare SQL query
  20. $sql = 'SELECT article_id, image_id, title, article
  21. FROM blog WHERE article_id = ?';
  22. $stmt->prepare($sql);
  23. // bind the query parameter
  24. $stmt->bind_param('i', $_GET['article_id']);
  25. // bind the results to variables
  26. $stmt->bind_result($article_id, $image_id, $title, $article);
  27. // execute the query, and fetch the result
  28. $OK = $stmt->execute();
  29. $stmt->fetch();
  30. // free the database resource for the next query
  31. $stmt->free_result();
  32. // get categories associated with the article
  33. $sql = 'SELECT cat_id FROM article2cat
  34. WHERE article_id = ?';
  35. $stmt->prepare($sql);
  36. $stmt->bind_param('i', $_GET['article_id']);
  37. $stmt->bind_result($cat_id);
  38. $OK = $stmt->execute();
  39. // loop through the results to store them in an array
  40. $selected_categories = array();
  41. while ($stmt->fetch()) {
  42. $selected_categories[] = $cat_id;
  43. }
  44. }
  45. // if form has been submitted, update record
  46. if (isset($_POST ['update'])) {
  47. // prepare update query
  48. if (!empty($_POST['image_id'])) {
  49. $sql = 'UPDATE blog SET image_id = ?, title = ?, article = ?
  50. WHERE article_id = ?';
  51. $stmt->prepare($sql);
  52. $stmt->bind_param('issi', $_POST['image_id'], $_POST['title'], $_POST['article'], $_POST['article_id']);
  53. } else {
  54. $sql = 'UPDATE blog SET image_id = NULL, title = ?, article = ?
  55. WHERE article_id = ?';
  56. $stmt->prepare($sql);
  57. $stmt->bind_param('ssi', $_POST['title'], $_POST['article'], $_POST['article_id']);
  58. }
  59. $stmt->execute();
  60. $done = $stmt->execute();
  61. // delete existing values in the cross-reference table
  62. $sql = 'DELETE FROM article2cat WHERE article_id = ?';
  63. $stmt->prepare($sql);
  64. $stmt->bind_param('i', $_POST['article_id']);
  65. $stmt->execute();
  66. // insert the new values in articles2cat
  67. if (isset($_POST['category']) && is_numeric($_POST['article_id'])) {
  68. $article_id = (int) $_POST['article_id'];
  69. foreach ($_POST['category'] as $cat_id) {
  70. $values[] = "($article_id, " . (int) $cat_id .')';
  71. }
  72. if ($values) {
  73. $sql = 'INSERT INTO article2cat (article_id, cat_id)
  74. VALUES ' . implode (',', $values);
  75. if (!$conn->query($sql)) {
  76. $catError = $conn->error;
  77. }
  78. }
  79. }
  80. }
  81. // redirect if $_GET['article_id'] not defined
  82. if ($done || !isset($_GET['article_id'])) {
  83. header('Location: http://localhost/phpsols/admin/blog_list_mysqli.php');
  84. exit;
  85. }
  86. // store error message if query fails
  87. if (isset($stmt) && !$OK && !$done) {
  88. $error = $stmt->error;
  89. }
  90. ?>
  91. <!DOCTYPE HTML>
  92. <html>
  93. <head>
  94. <meta charset="utf-8">
  95. <title>Update Blog Entry</title>
  96. <link href="../styles/admin.css" rel="stylesheet" type="text/css">
  97. </head>
  98. <body>
  99. <h1>Update Blog Entry</h1>
  100. <p><a href="blog_list_mysqli.php">List all entries </a></p>
  101. <?php
  102. if (isset($error)) {
  103. echo "<p class='warning'>Error: $error</p>";
  104. }
  105. if($article_id == 0) { ?>
  106. <p class="warning">Invalid request: record does not exist.</p>
  107. <?php } else { ?>
  108. <form id="form1" method="post" action="">
  109. <p>
  110. <label for="title">Title:</label>
  111. <input name="title" type="text" class="widebox" id="title" value="<?php echo htmlentities($title, ENT_COMPAT, 'utf-8'); ?>">
  112. </p>
  113. <p>
  114. <label for="article">Article:</label>
  115. <textarea name="article" cols="60" rows="8" class="widebox" id="article"><?php echo htmlentities($article, ENT_COMPAT, 'utf-8'); ?></textarea>
  116. </p>
  117. <p>
  118. <label for="category">Categories:</label>
  119. <select name="category[]" size="5" multiple id="category">
  120. <?php
  121. // get categories
  122. $getCats = 'SELECT cat_id, category FROM categories
  123. ORDER BY category';
  124. $categories = $conn->query($getCats);
  125. while ($row = $categories->fetch_assoc()) {
  126. ?>
  127. <option value="<?php echo $row['cat_id']; ?>" <?php
  128. if (in_array($row['cat_id'], $selected_categories)) {
  129. echo 'selected';
  130. } ?>><?php echo $row['category']; ?></option>
  131. <?php } ?>
  132. </select>
  133. </p>
  134. <p>
  135. <label for="image_id">Uploaded image:</label>
  136. <select name="image_id" id="image_id">
  137. <option value="">Select image</option>
  138. <?php
  139. // get the list of images
  140. $getImages = 'SELECT image_id, filename
  141. FROM images ORDER BY filename';
  142. $images = $conn->query($getImages);
  143. while ($row = $images->fetch_assoc()) {
  144. ?>
  145. <option value="<?php echo $row['image_id']; ?>"
  146. <?php
  147. if ($row['image_id'] == $image_id) {
  148. echo 'selected';
  149. }
  150. ?>><?php echo $row['filename']; ?></option>
  151. <?php } ?>
  152. </select>
  153. </p>
  154. <p>
  155. <input type="submit" name="update" value="Update Entry" id="update">
  156. <input name="article_id" type="hidden" value="<?php echo $article_id; ?>">
  157. </p>
  158. </form>
  159. <?php } ?>
  160. </body>
  161. </html>