Advertisement
Guest User

Untitled

a guest
May 6th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html lang="en">
  4. <?php
  5.  
  6. require 'database.php';
  7.  
  8.  
  9.  
  10. $id = null;
  11.  
  12. if ( !empty($_GET['id'])) {
  13.  
  14. $id = $_REQUEST['id'];
  15.  
  16. }
  17.  
  18.  
  19.  
  20. if ( null==$id ) {
  21.  
  22. header("Location: adminpage.php");
  23.  
  24. }
  25.  
  26.  
  27.  
  28. if ( !empty($_POST)) {
  29.  
  30. // keep track validation errors
  31.  
  32. $ISBN_noError = null;
  33.  
  34. $book_titleError = null;
  35.  
  36. $author_nameError = null;
  37.  
  38. $year_publishedError = null;
  39.  
  40. $priceError = null;
  41.  
  42. $publisherError = null;
  43.  
  44. $genreError = null;
  45.  
  46.  
  47.  
  48. // keep track post values
  49.  
  50. $ISBN_no = $_POST['ISBN_no'];
  51.  
  52. $book_title = $_POST['book_title'];
  53.  
  54. $author_name = $_POST['author_name'];
  55.  
  56. $year_published = $_POST['year_published'];
  57.  
  58. $price = $_POST['price'];
  59.  
  60. $publisher = $_POST['publisher'];
  61.  
  62. $genre = $_POST['genre'];
  63.  
  64.  
  65.  
  66. // validate input
  67.  
  68. if (empty($ISBN_no)) {
  69. $ISBN_noError = 'Please enter ISBN number.';
  70. $valid = false;
  71. }
  72.  
  73.  
  74. if (empty($book_title)) {
  75. $book_titleError = 'Please enter book title.';
  76. $valid = false;
  77. }
  78.  
  79.  
  80. if (empty($author_name)) {
  81. $authorError = 'Please enter the author name.';
  82. $valid = false;
  83. }
  84.  
  85. if (empty($year_published)) {
  86. $year_publishedError = 'Please enter the year the book was published.';
  87. $valid = false;
  88. }
  89.  
  90. if (empty($price)) {
  91. $priceError = 'Please enter price of the book.';
  92. $valid = false;
  93. }
  94.  
  95. if (empty($publisher)) {
  96. $publisherError = 'Please enter the name of the publisher.';
  97. $valid = false;
  98. }
  99.  
  100. if (empty($genre)) {
  101. $genreError = 'Please enter the type of genre the book is.';
  102. $valid = false;
  103. }
  104.  
  105. if (empty($image)) {
  106. $imageError = 'Please insert image of the book.';
  107. $valid = false;
  108. }
  109.  
  110.  
  111.  
  112. // update data
  113.  
  114. if ($valid) {
  115.  
  116. $pdo = Database::connect();
  117.  
  118. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  119.  
  120. $sql = "UPDATE books set ISBN_no = ?, book_title = ?, author_name =?, year_published = ?, price = ?, publisher = ?, genre = ?";
  121.  
  122. $q = $pdo->prepare($sql);
  123.  
  124. $q->execute(array($ISBN_no,$book_title,$author_name,$year_published,$price,$publisher,$genre));
  125.  
  126. Database::disconnect();
  127.  
  128. header("Location: adminpage.php");
  129.  
  130. }
  131.  
  132. } else {
  133.  
  134. $pdo = Database::connect();
  135.  
  136. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  137.  
  138. $sql = "SELECT * FROM books where id = ?";
  139.  
  140. $q = $pdo->prepare($sql);
  141.  
  142. $q->execute(array($id));
  143.  
  144. $data = $q->fetch(PDO::FETCH_ASSOC);
  145.  
  146. $ISBN_no = $data['ISBN_no'];
  147.  
  148. $book_title = $data['book_title'];
  149.  
  150. $author_name = $data['author_name'];
  151.  
  152. $year_published = $data['year_published'];
  153.  
  154. $price = $data['price'];
  155.  
  156. $publisher = $data['publisher'];
  157.  
  158. $genre = $data['genre'];
  159.  
  160. Database::disconnect();
  161.  
  162. }
  163.  
  164. ?>
  165.  
  166. <head>
  167.  
  168. <meta charset="utf-8">
  169.  
  170. <link href="css/bootstrap.min.css" rel="stylesheet">
  171.  
  172. <script src="js/bootstrap.min.js"></script>
  173.  
  174. </head>
  175.  
  176.  
  177.  
  178. <body>
  179.  
  180. <div class="container">
  181.  
  182.  
  183.  
  184. <div class="span10 offset1">
  185.  
  186. <div class="row">
  187.  
  188. <h3>Update a Book's details</h3>
  189.  
  190. </div>
  191.  
  192.  
  193.  
  194. <form class="form-horizontal" action="update.php?id=<?php echo $id?>" method="post">
  195.  
  196. <div class="control-group <?php echo !empty($ISBN_noError)?'error':'';?>">
  197.  
  198. <label class="control-label">ISBN Number</label>
  199.  
  200. <div class="controls">
  201.  
  202. <input name="ISBN_no" type="text" placeholder="ISBN_no" value="<?php echo !empty($ISBN_no)?$ISBN_no:'';?>">
  203.  
  204. <?php if (!empty($ISBN_noError)): ?>
  205.  
  206. <span class="help-inline"><?php echo $ISBN_noError;?></span>
  207.  
  208. <?php endif; ?>
  209.  
  210. </div>
  211.  
  212. </div>
  213.  
  214. <div class="control-group <?php echo !empty($book_titleError)?'error':'';?>">
  215.  
  216. <label class="control-label">Title</label>
  217.  
  218. <div class="controls">
  219.  
  220. <input name="book_title" type="text" placeholder="book_title" value="<?php echo !empty($book_title)?$book_title:'';?>">
  221.  
  222. <?php if (!empty($book_titleError)): ?>
  223.  
  224. <span class="help-inline"><?php echo $book_titleError;?></span>
  225.  
  226. <?php endif;?>
  227.  
  228. </div>
  229.  
  230. </div>
  231.  
  232. <div class="control-group <?php echo !empty($author_nameError)?'error':'';?>">
  233.  
  234. <label class="control-label">Author</label>
  235.  
  236. <div class="controls">
  237.  
  238. <input name="author_name" type="text" placeholder="author_name" value="<?php echo !empty($author_name)?$author_name:'';?>">
  239.  
  240. <?php if (!empty($author_nameError)): ?>
  241.  
  242. <span class="help-inline"><?php echo $author_nameError;?></span>
  243.  
  244. <?php endif;?>
  245.  
  246. </div>
  247.  
  248. </div>
  249.  
  250. <div class="control-group <?php echo !empty($year_publishedError)?'error':'';?>">
  251.  
  252. <label class="control-label">Year Pub.</label>
  253.  
  254. <div class="controls">
  255.  
  256. <input name="year_published" type="text" placeholder="year_published" value="<?php echo !empty($year_published)?$year_published:'';?>">
  257.  
  258. <?php if (!empty($author_publishedError)): ?>
  259.  
  260. <span class="help-inline"><?php echo $author_publishedError;?></span>
  261.  
  262. <?php endif; ?>
  263.  
  264. </div>
  265.  
  266. </div>
  267.  
  268. <div class="control-group <?php echo !empty($priceError)?'error':'';?>">
  269.  
  270. <label class="control-label">Price £</label>
  271.  
  272. <div class="controls">
  273.  
  274. <input name="price" type="text" placeholder="price" value="<?php echo !empty($price)?$price:'';?>">
  275.  
  276. <?php if (!empty($priceError)): ?>
  277.  
  278. <span class="help-inline"><?php echo $priceError;?></span>
  279.  
  280. <?php endif; ?>
  281.  
  282. </div>
  283.  
  284. </div>
  285.  
  286. <div class="control-group <?php echo !empty($publisherError)?'error':'';?>">
  287.  
  288. <label class="control-label">Publisher</label>
  289.  
  290. <div class="controls">
  291.  
  292. <input name="publisher" type="text" placeholder="publisher" value="<?php echo !empty($publisher)?$publisher:'';?>">
  293.  
  294. <?php if (!empty($publisherError)): ?>
  295.  
  296. <span class="help-inline"><?php echo $publisherError;?></span>
  297.  
  298. <?php endif; ?>
  299.  
  300. </div>
  301.  
  302. </div>
  303.  
  304. <div class="control-group <?php echo !empty($genreError)?'error':'';?>">
  305.  
  306. <label class="control-label">Genre</label>
  307.  
  308. <div class="controls">
  309.  
  310. <input name="genre" type="text" placeholder="genre" value="<?php echo !empty($genre)?$genre:'';?>">
  311.  
  312. <?php if (!empty($genreError)): ?>
  313.  
  314. <span class="help-inline"><?php echo $genreError;?></span>
  315.  
  316. <?php endif; ?>
  317.  
  318. </div>
  319.  
  320. </div>
  321.  
  322. <div class="form-actions">
  323.  
  324. <button type="submit" class="btn btn-success">Update</button>
  325.  
  326. <a class="btn" href="adminpage.php">Back</a>
  327.  
  328. </div>
  329.  
  330. </form>
  331.  
  332. </div>
  333.  
  334.  
  335.  
  336. </div> <!-- /container -->
  337.  
  338. </body>
  339.  
  340. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement