Guest User

Untitled

a guest
Dec 9th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. <?php
  2. /*
  3. Allows the user to both create new records and edit existing records
  4. */
  5.  
  6. // connect to the database
  7. include('../configs.php');
  8.  
  9. // creates the new/edit record form
  10. // since this form is used multiple times in this file, I have made it a function that is easily reusable
  11. function renderForm($first = '', $second ='', $third ='', $fourth ='', $fifth ='', $last ='', $error = '', $id = '')
  12. { ?>
  13. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  14. <html>
  15. <head>
  16. <title>
  17. <?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
  18. </title>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  20. </head>
  21. <body>
  22. <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
  23. <?php if ($error != '') {
  24. echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
  25. . "</div>";
  26. } ?>
  27.  
  28. <form action="" method="post">
  29. <div>
  30. <?php if ($id != '') { ?>
  31. <input type="hidden" name="id" value="<?php echo $id; ?>" />
  32. <p>ID: <?php echo $id; ?></p>
  33. <?php } ?>
  34.  
  35. <strong>Email: *</strong> <input type="text" name="email" value="<?php echo $first; ?>"/><br/>
  36.  
  37. <strong>bTag: *</strong> <input type="text" name="lastname" value="<?php echo $second; ?>"/><br/>
  38.  
  39. <strong>First Name: *</strong> <input type="text" name="lastname" value="<?php echo $third; ?>"/><br/>
  40.  
  41. <strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $fourth; ?>"/><br/>
  42.  
  43. <strong>Rank: *</strong> <input type="text" name="lastname" value="<?php echo $fifth; ?>"/><br/>
  44.  
  45. <strong>Avatar: *</strong> <input type="text" name="lastname" value="<?php echo $last; ?>"/>
  46. <p>* required</p>
  47. <input type="submit" name="submit" value="Submit" />
  48. </div>
  49. </form>
  50. </body>
  51. </html>
  52.  
  53. <?php }
  54.  
  55.  
  56.  
  57. /*
  58.  
  59. EDIT RECORD
  60.  
  61. */
  62. // if the 'id' variable is set in the URL, we know that we need to edit a record
  63. if (isset($_GET['id']))
  64. {
  65. // if the form's submit button is clicked, we need to process the form
  66. if (isset($_POST['submit']))
  67. {
  68. // make sure the 'id' in the URL is valid
  69. if (is_numeric($_POST['id']))
  70. {
  71. // get variables from the URL/form
  72. $id = $_POST['id'];
  73. $emailTag = htmlentities($_POST['email'], ENT_QUOTES);
  74. $bTag = htmlentities($_POST['bTag'], ENT_QUOTES);
  75. $firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
  76. $lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
  77. $rankTag = htmlentities($_POST['rank'], ENT_QUOTES);
  78. $avatar = htmlentities($_POST['avatar'], ENT_QUOTES);
  79.  
  80. // check that firstname and lastname are both not empty
  81. if ($emailTag == '' || $bTag == '' || $firstname == '' || $lastname == '' || $rankTag == '' || $avatar == '')
  82. {
  83. // if they are empty, show an error message and display the form
  84. $error = 'ERROR: Please fill in all required fields!';
  85. renderForm($emailTag, $bTag, $firstname, $lastname, $rankTag, $avatar, $error, $id);
  86. }
  87. else
  88. {
  89. // if everything is fine, update the record in the database
  90. if ($stmt = $aquaglz->prepare("UPDATE users SET email = ?, bTag = ?, firstname = ?, lastname = ?, rankTag = ?, avatar = ? WHERE uid=?"))
  91. {
  92. $stmt->bind_param("ssssisi", $emailTag, $bTag, $firstname, $lastname, $rankTag, $avatar, $id);
  93. $stmt->execute();
  94. $stmt->close();
  95. }
  96. // show an error message if the query has an error
  97. else
  98. {
  99. echo "ERROR: could not prepare SQL statement.";
  100. }
  101.  
  102. // redirect the user once the form is updated
  103. header("Location: view.php");
  104. }
  105. }
  106. // if the 'id' variable is not valid, show an error message
  107. else
  108. {
  109. echo "Error!";
  110. }
  111. }
  112. // if the form hasn't been submitted yet, get the info from the database and show the form
  113. else
  114. {
  115. // make sure the 'id' value is valid
  116. if (is_numeric($_GET['id']) && $_GET['id'] > 0)
  117. {
  118. // get 'id' from URL
  119. $id = $_GET['id'];
  120.  
  121. // get the recod from the database
  122. if($stmt = $aquaglz->prepare("SELECT uid, email, bTag, firstname, lastname, rank, avatar FROM users WHERE uid=?"))
  123. {
  124. $stmt->bind_param("i", $id);
  125. $stmt->execute();
  126.  
  127. $stmt->bind_result($id, $emailTag, $bTag, $firstname, $lastname, $rankTag, $avatar);
  128. $stmt->fetch();
  129.  
  130. // show the form
  131. renderForm($emailTag, $bTag, $firstname, $lastname, $rankTag, $avatar, NULL, $id);
  132.  
  133. $stmt->close();
  134. }
  135. // show an error if the query has an error
  136. else
  137. {
  138. echo "Error: could not prepare SQL statement";
  139. }
  140. }
  141. // if the 'id' value is not valid, redirect the user back to the view.php page
  142. else
  143. {
  144. header("Location: view.php");
  145. }
  146. }
  147. }
  148.  
  149.  
  150.  
  151. /*
  152.  
  153. NEW RECORD
  154.  
  155. */
  156. // if the 'id' variable is not set in the URL, we must be creating a new record
  157. else
  158. {
  159. // if the form's submit button is clicked, we need to process the form
  160. if (isset($_POST['submit']))
  161. {
  162. // get the form data
  163. $emailTag = htmlentities($_POST['email'], ENT_QUOTES);
  164. $bTag = htmlentities($_POST['bTag'], ENT_QUOTES);
  165.  
  166. // check that firstname and lastname are both not empty
  167. if ($emailTag == '' || $bTag == '' || $firstname == '' || $lastname == '' || $rankTag == '' || $avatar == '')
  168. {
  169. // if they are empty, show an error message and display the form
  170. $error = 'ERROR: Please fill in all required fields!';
  171. renderForm($emailTag, $bTag, $firstname, $lastname, $rankTag, $avatar, $error);
  172. }
  173. else
  174. {
  175. // insert the new record into the database
  176. if ($stmt = $aquaglz->prepare("INSERT users (email, bTag, firstname, lastname, rank, avatar) VALUES (?, ?)"))
  177. {
  178. $stmt->bind_param("ssssis", $emailTag, $bTag, $firstname, $lastname, $rankTag, $avatar);
  179. $stmt->execute();
  180. $stmt->close();
  181. }
  182. // show an error if the query has an error
  183. else
  184. {
  185. echo "ERROR: Could not prepare SQL statement.";
  186. }
  187.  
  188. // redirec the user
  189. header("Location: view.php");
  190. }
  191.  
  192. }
  193. // if the form hasn't been submitted yet, show the form
  194. else
  195. {
  196. renderForm();
  197. }
  198. }
  199.  
  200. // close the mysqli connection
  201. $aquaglz->close();
  202. ?>
Advertisement
Add Comment
Please, Sign In to add comment