Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. <?php
  2.  
  3. function validate_subject($user) {
  4. $errors = [];
  5.  
  6. // menu_name
  7. if(is_blank($user['email'])) {
  8. $errors[] = "E-mail cannot be blank.";
  9. }
  10.  
  11. // name
  12. if(is_blank($user['name'])) {
  13. $errors[] = "Name cannot be blank.";
  14. }
  15.  
  16. if(($user['password'] != $user['psw-repeat'])) {
  17. $errors[] = "Password must match.";
  18. }
  19.  
  20. return $errors;
  21. }
  22.  
  23.  
  24.  
  25. function find_all_users() {
  26. global $db;
  27. $query = "SELECT * FROM users";
  28. $result = mysqli_query($db, $query);
  29. return $result;
  30. }
  31.  
  32. function find_single_user($id) {
  33. global $db;
  34. $query = "SELECT * FROM users ";
  35. $query .= "WHERE id='" . $id . "'";
  36. $result = mysqli_query($db, $query);
  37. $user = mysqli_fetch_assoc($result);
  38. mysqli_free_result($result);
  39. return $user;
  40. }
  41.  
  42. // has_valid_date_format(array("full_name"->"name", "isMale"->True, "phone"->"10280191", "dob"-."dob", "email"->"tom@gmail.com", "address"->"24 Hartforde Rd")
  43. // * Inserts a user into the datbaase
  44. // * Requires full name, gender, phone number, date of birth, email, address
  45. function insert_user($user_array) {
  46. global $db;
  47.  
  48. $errors = validate_user($user_array);
  49. if (!empty($errors)) {
  50. return $errors;
  51. }
  52.  
  53. $query = "INSERT INTO users ";
  54. $query .= "(full_name, isMale, phone, dob, email, address) ";
  55. $query .= "VALUES (";
  56. $query .= "'" . $user_array['full_name'] . "',";
  57. $query .= $user_array['isMale'] . ",";
  58. $query .= "'" . $user_array['phone'] . "',";
  59. $query .= "'" . $user_array['dob'] . "',";
  60. $query .= "'" . $user_array['email'] . "'";
  61. $query .= "'" . $user_array['address'] . "'";
  62. $query .= ")";
  63. $result = mysqli_query($db, $query);
  64.  
  65. // INSERT succeeded
  66. if ($result) {
  67. return true;
  68. } else {
  69. // INSERT failed
  70. echo mysqli_error($db);
  71. db_disconnect($db);
  72. exit;
  73. }
  74. }
  75.  
  76. function validate_user($user) {
  77. $errors = [];
  78.  
  79. // name
  80. if (is_blank($user['full_name'])) {
  81. $errors[] = "Name cannot be blank.";
  82. } elseif(!has_length($user['full_name'], ['min' => 2, 'max' => 255])) {
  83. $errors[] = "Name must be between 2 and 255 characters.";
  84. }
  85.  
  86. if (is_blank($user['isMale'])) {
  87. $errors[] = "Gender can't be blank.";
  88. }
  89.  
  90. if (is_blank($user['phone'])) {
  91. $errors[] = "Phone number can't be blank.";
  92. } elseif(!has_length($user['phone'], ['min' => 6, 'max' => 12])) {
  93. $errors = "Phone number must be between 6 and 12 digits";
  94. }
  95.  
  96. if (is_blank($user['dob'])) {
  97. $errors[] = "Date of birth can't be blank.";
  98. } elseif(!has_valid_date_format($user['dob'])) {
  99. $errors[] = "Date must be in correct format: yyyy-mm-dd";
  100. } elseif (!date_in_past($user['dob'])) {
  101. $errors[] = "Date must be in past";
  102. }
  103.  
  104. if (is_blank($user['email'])) {
  105. $errors[] = "Email can't be blank.";
  106. } elseif(!has_length($user['email'], ['min' => 3, 'max' => 255])) {
  107. $errors = "Email must be between 2 and 255 characters.";
  108. } elseif(!has_valid_email_format($user['email'])) {
  109. $errors = "Email must be in a valid format eg. tom@gmail.com";
  110. }
  111.  
  112. if (is_blank($user['password'])) {
  113. $errors[] = "Password can't be blank.";
  114. } elseif(!haslength($user['password'], ['min' => 5, 'max' => 255])) {
  115. $errors = "Password must be between 5 and 255 characters.";
  116. }
  117.  
  118. if (is_blank($user['address'])) {
  119. $errors[] = "Address can't be blank.";
  120. }
  121. return $errors;
  122. }
  123.  
  124. function delete_user($id) {
  125. global $db;
  126. $query = "DELETE from users ";
  127. $query .= "WHERE id=" . "'" . $id . "'";
  128. $result = mysqli_query($db, $query);
  129. }
  130.  
  131. function update_user($user_array) {
  132. global $db;
  133. $query = "UPDATE users ";
  134. $query .= "SET full_name = ";
  135. $query.= "'" . $user_array['full_name'] . "'";
  136. $query .= "'" . $user_array['email'] . "'";
  137. $query .= "'" . $user_array['phone'] . "'";
  138. $query .= "'" . $user_array['gender'] . "'";
  139. $query .= "'" . $user_array['dob'] . "'";
  140. $query .= "'" . $user_array['rating'] . "'";
  141. $query .= "'" . $user_array['address'] . "'";
  142. $query .= " WHERE id = ";
  143. $query .= "'" . $id . "'";
  144. }
  145.  
  146. // Require at least one organiser?
  147. function insert_tournament($name, $location) {
  148. global $db;
  149. $query = "INSERT INTO tournaments ";
  150. $query .= "(name, location) ";
  151. $query .= "VALUES (";
  152. $query .= "'" . $name . "'";
  153. $query .= "'" . $location . "'";
  154. $query .= ")";
  155. $result = mysqli_query($db, $query);
  156. return $result;
  157. }
  158.  
  159. function find_single_tournament($id) {
  160. global $db;
  161. $query = "SELECT * FROM tournaments ";
  162. $query .= "WHERE id='" . $id . "'";
  163. $result = mysqli_query($db, $query);
  164. $tournament = mysqli_fetch_assoc($result);
  165. mysqli_free_result($result);
  166. return $team;
  167. }
  168.  
  169. function find_all_tournaments() {
  170. global $db;
  171. $query = "SELECT * FROM tournaments ";
  172. $result = mysqli_query($db, $query);
  173. return $result;
  174. }
  175.  
  176. function insert_officer($id) {
  177. global $db;
  178. $query = "INSERT INTO officers ";
  179. $qurey .= "(user_id) ";
  180. $query .= "VALUES (";
  181. $query .= "'" .$id . "'";
  182. $query .= ")";
  183. $result = mysqli_query($db, $query);
  184. return $result;
  185. }
  186.  
  187. function find_single_officer($id) {
  188. global $db;
  189. $query = "SELECT * FROM officers ";
  190. $query .= "WHERE user_id='". $id ."'";
  191. $result = mysqli_query($db, $query);
  192. $officer = mysqli_fetch_assoc($result);
  193. mysqli_free_result($result);
  194. return $officer;
  195. }
  196.  
  197. function find_all_officers() {
  198. global $db;
  199. $query = "SELECT * FROM officers ";
  200. $result = mysqli_query($db, $query);
  201. return $result;
  202. }
  203.  
  204. function find_single_match($id) {
  205. global $db;
  206. $query = "SELECT * FROM matches ";
  207. $query .= "WHERE id='" . $id . "'";
  208. $result = mysqli_query($db, $query);
  209. $match = mysqli_fetch_assoc($result);
  210. mysqli_free_result($result);
  211. return $match;
  212. }
  213.  
  214. function insert_match($match_array) {
  215. global $db;
  216.  
  217. /*
  218. $errors = validate_user($user_array);
  219. if (!empty($errors)) {
  220. return $errors;
  221. }
  222. */
  223.  
  224. $query = "INSERT INTO matches ";
  225. $query .= "(tournament_id, player1_id, player2_id, date_played) ";
  226. $query .= "VALUES (";
  227. $query .= "'" . $user_array['tournament_id'] . "',";
  228. $query .= "'" . $user_array['player1_id'] . "',";
  229. $query .= "'" . $user_array['player2_id'] . "',";
  230. $query .= "'" . $user_array['dated_played'] . "'";
  231. $query .= ")";
  232. $result = mysqli_query($db, $query);
  233.  
  234. // INSERT succeeded
  235. if ($result) {
  236. return true;
  237. } else {
  238. // INSERT failed
  239. echo mysqli_error($db);
  240. db_disconnect($db);
  241. exit;
  242. }
  243. }
  244.  
  245. function find_all_matches() {
  246. global $db;
  247. $query = "SELECT * FROM MATCHES ";
  248. $result = mysqli_query($db, $query);
  249. return $result;
  250. }
  251. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement