Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. <?php include "includes/admin_header.php"; ?>
  2.  
  3. <h1>Welcome to Admin Page</h1>
  4. <hr>
  5.  
  6. <table class="table table-bordered">
  7. <thead class="thead-dark">
  8. <tr>
  9. <th>ID</th>
  10. <th>Post Title</th>
  11. <th>Category</th>
  12. <th>Author</th>
  13. <th>Date</th>
  14. <th>Comments</th>
  15. <th>Image</th>
  16. <th>Text</th>
  17. <th>Tags</th>
  18. <th>Actions</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22.  
  23. <?php
  24. if(isset($_POST["add_post"])){
  25. $post_title = htmlspecialchars($_POST["post_title"]);
  26. $post_category = htmlspecialchars($_POST["post_category"]);
  27. $post_author = htmlspecialchars($_POST["post_author"]);
  28. $post_tags = htmlspecialchars($_POST["post_tags"]);
  29. $post_text = htmlspecialchars($_POST["post_text"]);
  30. $post_date = date("d-m-y");
  31. $post_comment_number = 8;
  32.  
  33. $post_image = $_FILES["post_image"]["name"];
  34. $post_image_temp = $_FILES["post_image"]["tmp_name"];
  35.  
  36. move_uploaded_file($post_image_temp, "../img/$post_image");
  37. $query = "INSERT INTO posts (post_title,post_category,post_author,post_text,post_tags,post_date,
  38. post_comment_number,post_image)";
  39. $query .= "VALUES('{$post_title}','{$post_category}','{$post_author}','{$post_text}','{$post_tags}',now()
  40. ,'{$post_comment_number}','{$post_image}')";
  41.  
  42. $create_post_query = mysqli_query($conn, $query);
  43. header("Location: posts.php");
  44. }
  45.  
  46. ?>
  47.  
  48. <?php
  49. if(isset($_POST["edit_post"])){
  50. $post_title = htmlspecialchars($_POST["post_title"]);
  51. $post_category = htmlspecialchars($_POST["post_category"]);
  52. $post_author = htmlspecialchars($_POST["post_author"]);
  53. $post_tags = htmlspecialchars($_POST["post_tags"]);
  54. $post_text = htmlspecialchars($_POST["post_text"]);
  55. $post_id = htmlspecialchars($_POST["post_id"]);
  56.  
  57.  
  58. $post_image = htmlspecialchars($_FILES["post_image"]["name"]);
  59. $post_image_temp = htmlspecialchars($_FILES["post_image"]["tmp_name"]);
  60.  
  61. move_uploaded_file($post_image_temp, "../img/$post_image");
  62.  
  63. if(empty($post_image)) {
  64. $query = "SELECT * FROM posts WHERE post_id = '$_POST[post_id]'";
  65. $select_image = mysqli_query($conn, $query);
  66. while($row = mysqli_fetch_array($select_image)){
  67. $post_image = htmlspecialchars($row["post_image"]);
  68. }
  69.  
  70. }
  71.  
  72. $sql_query2 = "UPDATE posts SET post_title = '$post_title', post_category = '$post_category',
  73. post_author = '$post_author', post_tags = '$post_tags', post_text = '$post_text', post_image = '$post_image'
  74. WHERE post_id = '$post_id'";
  75.  
  76. $edit_post_query = mysqli_query($conn, $sql_query2);
  77. header("Location: posts.php");
  78.  
  79. }
  80. ?>
  81.  
  82. <?php
  83. $sql_query = "SELECT * FROM posts ORDER BY post_id DESC";
  84. $select_all_posts = mysqli_query($conn, $sql_query);
  85. $post = 1;
  86. while($row = mysqli_fetch_assoc($select_all_posts)){
  87. $post_id = htmlspecialchars($row["post_id"]);
  88. $post_category = htmlspecialchars($row["post_category"]);
  89. $post_title = htmlspecialchars($row["post_title"]);
  90. $post_author = htmlspecialchars($row["post_author"]);
  91. $post_date = htmlspecialchars($row["post_date"]);
  92. $post_comment_number = 8;
  93. $post_image = htmlspecialchars($row["post_image"]);
  94. $post_text = htmlspecialchars($row["post_text"]);
  95. $post_tags = htmlspecialchars($row["post_tags"]);
  96. echo "<tr>
  97. <td>{$post_id}</td>
  98. <td>{$post_title}</td>
  99. <td>{$post_category}</td>
  100. <td>{$post_author}</td>
  101. <td>{$post_date}</td>
  102. <td>{$post_comment_number}</td>
  103. <td><img src='../img/$post_image' width='100px' height='100px'></td>
  104. <td>{$post_text}</td>
  105. <td>{$post_tags}</td>
  106. <td>
  107. <div class='dropdown'>
  108. <button class='btn btn-primary dropdown-toggle' type='button' id='dropdownMenuButton'
  109. data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>
  110. Actions
  111. </button>
  112. <div class='dropdown-menu' aria-labelledby='dropdownMenuButton'>
  113. <a class='dropdown-item' data-toggle='modal' data-target='#edit_modal$post' href='#'>Edit</a>
  114. <div class='dropdown-divider'></div>
  115. <a class='dropdown-item' href='posts.php?delete={$post_id}'>Delete</a>
  116. <div class='dropdown-divider'></div>
  117. <a class='dropdown-item' data-toggle='modal' href='#' data-target='#add_modal'>Add</a>
  118. </div>
  119. </div>
  120. </td>
  121. </tr>";
  122.  
  123. ?>
  124.  
  125.  
  126.  
  127. <div id="edit_modal<?php echo $post; ?>" class="modal fade">
  128. <div class="modal-dialog" role="document">
  129. <div class="modal-content">
  130. <div class="modal-header">
  131. <h5 class="modal-title" id="exampleModalLabel">Edit Post</h5>
  132. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  133. <span aria-hidden="true">&times;</span>
  134. </button>
  135. </div>
  136. <div class="modal-body">
  137. <form action="" method="post" enctype="multipart/form-data">
  138. <div class="form-group">
  139. <label for="post_title">Post Title</label>
  140. <input type="text" class="form-control" name="post_title" value="<?php echo $post_title ?>">
  141. </div>
  142. <div class="form-group">
  143. <label for="post_category">Post Category</label>
  144. <select class="form-group" name="post_category">
  145. <?php
  146. //edit
  147. $edit_category_sql = "SELECT * FROM categories ORDER BY category_id DESC";
  148. $post_category = $_POST["post_category"];
  149. $edit_cat_run = mysqli_query($conn, $edit_category_sql);
  150. while ($edit_cat_rows = mysqli_fetch_assoc($edit_cat_run)){
  151. $edited_category = htmlspecialchars($edit_cat_rows["category_name"]);
  152.  
  153. echo "<option>$edited_category</option>";
  154. }
  155. ?>
  156. </select>
  157. <div class="form-group">
  158. <label for="post_author">Post Author</label>
  159. <input type="text" class="form-control" name="post_author" value="<?php echo $post_author ?>">
  160. </div>
  161.  
  162. <div class="form-group">
  163. <label for="post_image">Post Image</label>
  164. <img width="100" height="100" src="../img/<?php echo $post_image ?>">
  165. <input type="file" class="form-control" name="post_image">
  166.  
  167. </div>
  168. <div class="form-group">
  169. <label for="post_tags">Post Tags</label>
  170. <input type="text" class="form-control" name="post_tags" value="<?php echo $post_tags ?>">
  171. </div>
  172. <div class="form-group">
  173. <label for="post_text">Post Text</label>
  174. <textarea class="form-control" name="post_text" id="" cols="20" rows="5"><?php echo $post_text ?></textarea>
  175. </div>
  176.  
  177. <div class="form-group">
  178. <input type="hidden" name="post_id" value="<?php $row["post_id"]; ?>">
  179. <input type="submit" class="btn btn-primary" name="edit_post" value="Edit Post">
  180. </div>
  181. </form>
  182. </div>
  183. </div>
  184. </div>
  185. </div>
  186. <?php $post++; } ?>
  187. </tbody>
  188. </table>
  189.  
  190.  
  191.  
  192. <div id="add_modal" class="modal fade">
  193. <div class="modal-dialog" role="document">
  194. <div class="modal-content">
  195. <div class="modal-header">
  196. <h5 class="modal-title" id="exampleModalLabel">Add New Category</h5>
  197. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  198. <span aria-hidden="true">&times;</span>
  199. </button>
  200. </div>
  201. <div class="modal-body">
  202. <form action="" method="post" enctype="multipart/form-data">
  203. <div class="form-group">
  204. <label for="post_title">Post Title</label>
  205. <input type="text" class="form-control" name="post_title">
  206. </div>
  207. <div class="form-group">
  208. <label for="post_category">Post Category</label>
  209. <select class="form-group" name="post_category">
  210. <?php
  211. //edit
  212. $edit_category_sql = "SELECT * FROM categories ORDER BY category_id DESC";
  213. $post_category = $_POST["post_category"];
  214. $edit_cat_run = mysqli_query($conn, $edit_category_sql);
  215. while ($edit_cat_rows = mysqli_fetch_assoc($edit_cat_run)){
  216. $edited_category = htmlspecialchars($edit_cat_rows["category_name"]);
  217.  
  218. echo "<option>$edited_category</option>";
  219.  
  220. }
  221. ?>
  222. </select>
  223. </div>
  224. <div class="form-group">
  225. <label for="post_author">Post Author</label>
  226. <input type="text" class="form-control" name="post_author">
  227. </div>
  228.  
  229. <div class="form-group">
  230. <label for="post_image">Post Image</label>
  231. <input type="file" class="form-control" name="post_image">
  232. </div>
  233. <div class="form-group">
  234. <label for="post_tags">Post Tags</label>
  235. <input type="text" class="form-control" name="post_tags">
  236. </div>
  237. <div class="form-group">
  238. <label for="post_text">Post Text</label>
  239. <textarea class="form-control" name="post_text" id="" cols="20" rows="5"></textarea>
  240. </div>
  241.  
  242. <div class="form-group">
  243. <input type="hidden" name="post_id" value="">
  244. <input type="submit" class="btn btn-primary" name="add_post" value="Add Post">
  245. </div>
  246. </form>
  247. </div>
  248. </div>
  249. </div>
  250. </div>
  251.  
  252. <?php
  253. if(isset($_GET["delete"])){
  254.  
  255. $del_post_id = $_GET["delete"];
  256.  
  257. $sql_query = "DELETE FROM posts WHERE post_id ={$del_post_id} ";
  258.  
  259. $delete_post_query = mysqli_query($conn, $sql_query);
  260. header("Location: posts.php");
  261. }
  262.  
  263. ?>
  264.  
  265. <?php include "includes/admin_footer.php"; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement