Advertisement
faygh2013

Untitled

Aug 9th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1.  
  2. <?php
  3. require_once '../config.php';
  4. require_once 'header.php';
  5. require_once 'logincheck.php';
  6.  
  7. $bookname_error=$author_error=$price_error=$category_error=$image_err="";
  8. $bookname=$author=$Price=$category=$image=$status="";
  9.  
  10. if(isset($_POST['submit'])){
  11.  
  12. //validate book name
  13. if(empty($_POST['book_name'])){
  14. $bookname_error="please enter book name";
  15.  
  16. }else{
  17. $bookname=test_input($_POST['book_name']);
  18. $name_pattern='/^[a-zA-Z]+$/';
  19. if(!preg_match($name_pattern,$bookname)){
  20. $bookname_error="please enter valid book name";
  21. }
  22. }
  23. //validate author name
  24. if(empty($_POST['author'])){
  25. $author_error="please enter author name";
  26.  
  27. }else{
  28. $author=test_input($_POST['author']);
  29. $author_pattern='/^[a-zA-Z]+$/';
  30. if(!preg_match($author_pattern,$author)){
  31. $$author_error="please enter valid author name";
  32. }
  33.  
  34. }
  35. //validate price
  36. if(empty($_POST['price'])){
  37. $price_error="please enter price";
  38.  
  39. }else{
  40. $price=test_input($_POST['price']);
  41. $price_pattern='/^[1-9]+$/';
  42. if(!preg_match($price_pattern,$price)){
  43. $price_error="please enter valid price";
  44. }
  45.  
  46. }
  47.  
  48. //validate price
  49. if(empty($_POST['category'])){
  50. $category_error="please enter category";
  51.  
  52. }else{
  53. $category=test_input($_POST['category']);
  54. $category_pattern='/^[a-zA-Z]+$/';
  55. if(!preg_match($category_pattern,$category)){
  56. $category_error="please enter valid category";
  57. }
  58.  
  59. }
  60.  
  61. //validate book image
  62. if(!isset($_FILES['book_img'])){
  63. $image_err="please select image";
  64. } else{
  65. $target="images/";
  66. $file_name=$_FILES['book_img']['name'];
  67. $file_type=$_FILES['book_img']['type'];
  68. $file_size=$_FILES['book_img']['size'];
  69. $temp_name=$_FILES['book_img']['tmp_name'];
  70. $allowed=array('jpg'=>'image/jpg','jpeg'=>'image/jpeg');
  71. if(!in_array($file_type,$allowed)){
  72. $image_err="please select jpg/jpeg file";
  73.  
  74. }
  75. $maxsize=1*1024*1024;
  76. if($file_size>$maxsize){
  77. $image_err="file size greater than 1 MB";
  78. }
  79. if(in_array($file_type,$allowed)&& $file_size<$maxsize && $_FILES['book_img']['error']===0){
  80. $newname=rand().$file_name;
  81. $target=$target.$newname;
  82. $image=$target;
  83. move_uploaded_file($temp_name,$target);
  84.  
  85.  
  86. }
  87. }
  88. if(empty($bookname_error)&& empty($author_error) && empty($price_error) && empty($category_error)&& empty($image_err))
  89. {
  90. $sql="INSERT INTO books values('','$bookname','$image','$author','','$price','$category')";
  91. if(mysqli_query($link,$sql)){
  92. $status='<div class="alert alert-success">successfully added book</div>';
  93.  
  94. }else{
  95. $status='<div class="alert alert-success">error adding books</div>';
  96. }
  97. }
  98. }
  99.  
  100. function test_input($data){
  101. $data=trim($data);
  102. $data=stripcslashes($data);
  103. $data=htmlspecialchars($data);
  104. return $data;
  105. }
  106. ?>
  107. <div class="container">
  108. <div class="row">
  109.  
  110. <div class="col-lg-12">
  111.  
  112. <div class="row">
  113. <div class="col-sm-3"></div>
  114. <div class="col-sm-5">
  115. <h4 class="text-warning">Provide below details to add book</h4><br>
  116. <span><?php echo $status ?></span>
  117. <form class="form" method="POST" enctype="multipart/form-data">
  118. <div class="form-group">
  119. <label for="">Name of Book</label>
  120. <input type="text" name="book_name" value="" class="form-control">
  121. <span class="text-danger"><?php echo $bookname_error; ?></span>
  122. </div>
  123.  
  124. <div class="form-group">
  125. <label for="">Author</label>
  126. <input type="text" name="author" value="" class="form-control">
  127. <span class="text-danger"><?php echo $author_error; ?></span>
  128. </div>
  129.  
  130. <div class="form-group">
  131. <label for="">Price</label>
  132. <input type="text" name="price" value="" class="form-control">
  133. <span class="text-danger"><?php echo $price_error; ?></span>
  134. </div>
  135.  
  136. <div class="form-group">
  137. <label for="">category</label>
  138. <input type="text" name="category" value="" class="form-control">
  139. <span class="text-danger"><?php echo $category_error; ?></span>
  140. </div>
  141.  
  142. <div class="form-group">
  143. <label for="">Upload Book Image</label>
  144. <input type="file" name="book_img" value="book_img" class="form-control">
  145. <span class="text-danger"><?php echo $image_err; ?></span>
  146. </div>
  147.  
  148. <div class="form-group">
  149. <input type="submit" name="submit" value="add book" class="btn btn-success">
  150. </div>
  151.  
  152. </form>
  153. </div>
  154. <div class="col-sm-4"></div>
  155. </div>
  156.  
  157. </div>
  158.  
  159.  
  160. </div>
  161. </div>
  162. <?php
  163. require_once 'footer.php';
  164. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement