Advertisement
Guest User

Untitled

a guest
May 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. <?php
  2.  
  3. $name="";
  4. $price=0;
  5. $categoryID=1;
  6. $topline="";
  7. $description="";
  8. $photo="noimage.png";
  9. $NameErr=$PriceErr=$PhotoErr=$TopErr=$DesErr="";
  10.  
  11. // Sql to populate our 'edit' form...
  12. $stockID=preg_replace('/[^0-9]/','',$_REQUEST['stockID']);
  13. $editstock_sql="SELECT * FROM stock WHERE stockID=".$stockID;
  14. $editstock_query=mysqli_query($dbconnect, $editstock_sql);
  15. $editstock_rs=mysqli_fetch_assoc($editstock_query);
  16.  
  17. $name=$editstock_rs['name'];
  18. $price=$editstock_rs['price'];
  19. $categoryID=$editstock_rs['categoryID'];
  20. $topline=$editstock_rs['topline'];
  21. $description=$editstock_rs['description'];
  22. $photo=$editstock_rs['photo'];
  23.  
  24. // define variable and set to empty values...
  25. $valid=true;
  26. $uploadOk = 1;
  27.  
  28. // Code below excutes when the form is submitted...
  29. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  30.  
  31.  
  32. // Sanitise all variables
  33. $name = test_input(mysqli_real_escape_string($dbconnect,$_POST["name"]));
  34. $price = test_input($_POST["price"]);
  35. $categoryID = preg_replace('/[^0-9.]/','',$_POST['categoryID']);
  36. $topline = test_input(mysqli_real_escape_string($dbconnect,$_POST["topline"]));
  37. $description = test_input(mysqli_real_escape_string($dbconnect,$_POST["description"]));
  38.  
  39. // Error checking...
  40. if (empty($name)) {
  41. $NameErr = "Item name is required";
  42. $valid=false;
  43. }
  44.  
  45. $price=preg_replace('/[^0-9.]-/','',$_POST['price']);
  46. if ($price<=0) {
  47. $PriceErr = "Enter a number greater than 0";
  48. $valid=false;
  49. }
  50.  
  51. if (empty($topline)) {
  52. $TopErr = "Please provide a byline";
  53. $valid=false;
  54. }
  55.  
  56. if (empty($description)) {
  57. $DesErr = "Please provide a description";
  58. $valid=false;
  59. }
  60.  
  61. // Check Image...
  62. if ($_FILES['fileToUpload']['name']!="") {
  63.  
  64. // Shifts images from temp directory to target directory
  65.  
  66. // use unique-id so each uploaded file is unique
  67. $target_file = uniqid()."-". basename($_FILES["fileToUpload"]['name']);
  68. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  69.  
  70. // Allow .jpg, .png or gif only
  71. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "gif" ) {
  72. $PhotoErr = "Sorry, only JPG, JPEG, PNG & GIF file are allowed.";
  73. $uploadOk = 0;
  74. $valid=false;
  75. }
  76.  
  77. // Check file size
  78. if ($_FILES["fileToUpload"]["size"] > 500000) {
  79. $PhotoErr = "Sorry, your file is too large.";
  80. $uploadOk = 0;
  81. $valid=false;
  82. }
  83.  
  84. }
  85.  
  86.  
  87.  
  88. // If everything is OK - show 'success' message and update database
  89. if($valid){
  90. header('Location: admin.php?page=editstock_success');
  91.  
  92. // Replace image and delete 'old' image if necessary
  93.  
  94. if ($_FILES['fileToUpload']['name']!="")
  95. {
  96. $target_file = uniqid()."-". basename($_FILES["fileToUpload"]['name']);
  97. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  98. $changephoto=",photo=\"$target_file\"";
  99.  
  100. // Removes old photo file...
  101. if ($editstock_rs['photo']!='noimage.png' and $editstock_rs['photo']!='')
  102. {
  103. unlink(IMAGE_DIRECTORY."/".$editstock_rs['photo']);
  104. }
  105.  
  106. $fileuploaded=1;
  107.  
  108. }
  109.  
  110. else {
  111. $fileuploaded=0;
  112. $changephoto='';
  113. }
  114.  
  115.  
  116. // Update the database Column_Name=New_Value,Column_Name=New_Value
  117.  
  118. $editstock_sql="UPDATE stock SET
  119. name='$name',
  120. categoryID='$categoryID',
  121. price='$price',
  122. photo='$photo',
  123. topline='$topline',
  124. description='$description'
  125. $changephoto
  126. WHERE stockID=$stockID";
  127.  
  128.  
  129.  
  130. // Code below runs query and inputs data into database
  131. $editstock_query=mysqli_query($dbconnect,$editstock_sql);
  132.  
  133. if ($uploadOk==1) {
  134.  
  135. move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],IMAGE_DIRECTORY.'/'.$target_file);
  136.  
  137. }
  138.  
  139. }
  140.  
  141.  
  142.  
  143. }
  144.  
  145. ?>
  146.  
  147. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]."?page=editstock&stockID=$stockID");?>" enctype="multipart/form-data">
  148.  
  149. <h1>Edit Item</h1>
  150.  
  151. <p>
  152. <b>Item Name:</b>
  153. <input type="text" name="name" value="<?php echo $name;?>" />
  154. &nbsp;&nbsp; <span class="error"><?php echo $NameErr;?></span>
  155. </p>
  156.  
  157. <p>
  158. <b>Price: $</b>
  159. <input type="text" name="price" value="<?php echo $price;?>" size="2" />
  160. &nbsp;&nbsp; <span class="error"><?php echo $PriceErr;?></span>
  161. </p>
  162.  
  163. <p>
  164. <b>Category</b>
  165. <select name="categoryID">
  166.  
  167. <?php
  168.  
  169. $cat_sql="SELECT * FROM category";
  170. $cat_query=mysqli_query($dbconnect, $cat_sql);
  171.  
  172. do {
  173.  
  174. if ($cat_rs['categoryID']==$categoryID) {
  175. echo '<option value="'.$cat_rs['categoryID'].'"selected';
  176. echo ">".$cat_rs['catName']."</option>";
  177. }
  178. else{
  179. echo '<option value="'.$cat_rs['categoryID'].'"';
  180. echo ">".$cat_rs['catName']."</option>";
  181. }
  182. }
  183. while ($cat_rs=mysqli_fetch_assoc($cat_query))
  184.  
  185. ?>
  186.  
  187. </select>
  188. </p>
  189.  
  190. <p>
  191. <b>Photo</b>
  192. <p>
  193. <?php
  194. // shows image in database
  195. echo "<img src=".IMAGE_DIRECTORY."/".$editstock_rs['photo'].">";
  196. ?>
  197. </p>
  198. Optionally Replace Photo Above:
  199. <input type="file" name="fileToUpload" id="fileToUpload" value="" />&nbsp;&nbsp; <span class="error"><?php echo $PhotoErr;?></span>
  200. </p>
  201.  
  202. <p>
  203. <b>Topline</b>
  204. <input type="text" name="topline" value="<?php echo $topline;?>" />
  205. &nbsp;&nbsp; <span class="error"><?php echo $TopErr;?></span>
  206. </p>
  207.  
  208. <p>
  209. <b>Description</b>&nbsp;&nbsp; <span class="error"><?php echo $DesErr;?></span>
  210. </p>
  211. <p>
  212. <textarea type="text" name="description" cols="60" rows="7"><?php echo $description; ?></textarea>
  213. </p>
  214.  
  215. <input type="submit" name="submit" value="Edit Item" />
  216.  
  217. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement