Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function validate_subject($user) {
- $errors = [];
- // menu_name
- if(is_blank($user['email'])) {
- $errors[] = "E-mail cannot be blank.";
- }
- // name
- if(is_blank($user['name'])) {
- $errors[] = "Name cannot be blank.";
- }
- if(($user['password'] != $user['psw-repeat'])) {
- $errors[] = "Password must match.";
- }
- return $errors;
- }
- function find_all_users() {
- global $db;
- $query = "SELECT * FROM users";
- $result = mysqli_query($db, $query);
- return $result;
- }
- function find_single_user($id) {
- global $db;
- $query = "SELECT * FROM users ";
- $query .= "WHERE id='" . $id . "'";
- $result = mysqli_query($db, $query);
- $user = mysqli_fetch_assoc($result);
- mysqli_free_result($result);
- return $user;
- }
- // has_valid_date_format(array("full_name"->"name", "isMale"->True, "phone"->"10280191", "dob"-."dob", "email"->"tom@gmail.com", "address"->"24 Hartforde Rd")
- // * Inserts a user into the datbaase
- // * Requires full name, gender, phone number, date of birth, email, address
- function insert_user($user_array) {
- global $db;
- $errors = validate_user($user_array);
- if (!empty($errors)) {
- return $errors;
- }
- $query = "INSERT INTO users ";
- $query .= "(full_name, isMale, phone, dob, email, address) ";
- $query .= "VALUES (";
- $query .= "'" . $user_array['full_name'] . "',";
- $query .= $user_array['isMale'] . ",";
- $query .= "'" . $user_array['phone'] . "',";
- $query .= "'" . $user_array['dob'] . "',";
- $query .= "'" . $user_array['email'] . "'";
- $query .= "'" . $user_array['address'] . "'";
- $query .= ")";
- $result = mysqli_query($db, $query);
- // INSERT succeeded
- if ($result) {
- return true;
- } else {
- // INSERT failed
- echo mysqli_error($db);
- db_disconnect($db);
- exit;
- }
- }
- function validate_user($user) {
- $errors = [];
- // name
- if (is_blank($user['full_name'])) {
- $errors[] = "Name cannot be blank.";
- } elseif(!has_length($user['full_name'], ['min' => 2, 'max' => 255])) {
- $errors[] = "Name must be between 2 and 255 characters.";
- }
- if (is_blank($user['isMale'])) {
- $errors[] = "Gender can't be blank.";
- }
- if (is_blank($user['phone'])) {
- $errors[] = "Phone number can't be blank.";
- } elseif(!has_length($user['phone'], ['min' => 6, 'max' => 12])) {
- $errors = "Phone number must be between 6 and 12 digits";
- }
- if (is_blank($user['dob'])) {
- $errors[] = "Date of birth can't be blank.";
- } elseif(!has_valid_date_format($user['dob'])) {
- $errors[] = "Date must be in correct format: yyyy-mm-dd";
- } elseif (!date_in_past($user['dob'])) {
- $errors[] = "Date must be in past";
- }
- if (is_blank($user['email'])) {
- $errors[] = "Email can't be blank.";
- } elseif(!has_length($user['email'], ['min' => 3, 'max' => 255])) {
- $errors = "Email must be between 2 and 255 characters.";
- } elseif(!has_valid_email_format($user['email'])) {
- $errors = "Email must be in a valid format eg. tom@gmail.com";
- }
- if (is_blank($user['password'])) {
- $errors[] = "Password can't be blank.";
- } elseif(!haslength($user['password'], ['min' => 5, 'max' => 255])) {
- $errors = "Password must be between 5 and 255 characters.";
- }
- if (is_blank($user['address'])) {
- $errors[] = "Address can't be blank.";
- }
- return $errors;
- }
- function delete_user($id) {
- global $db;
- $query = "DELETE from users ";
- $query .= "WHERE id=" . "'" . $id . "'";
- $result = mysqli_query($db, $query);
- }
- function update_user($user_array) {
- global $db;
- $query = "UPDATE users ";
- $query .= "SET full_name = ";
- $query.= "'" . $user_array['full_name'] . "'";
- $query .= "'" . $user_array['email'] . "'";
- $query .= "'" . $user_array['phone'] . "'";
- $query .= "'" . $user_array['gender'] . "'";
- $query .= "'" . $user_array['dob'] . "'";
- $query .= "'" . $user_array['rating'] . "'";
- $query .= "'" . $user_array['address'] . "'";
- $query .= " WHERE id = ";
- $query .= "'" . $id . "'";
- }
- // Require at least one organiser?
- function insert_tournament($name, $location) {
- global $db;
- $query = "INSERT INTO tournaments ";
- $query .= "(name, location) ";
- $query .= "VALUES (";
- $query .= "'" . $name . "'";
- $query .= "'" . $location . "'";
- $query .= ")";
- $result = mysqli_query($db, $query);
- return $result;
- }
- function find_single_tournament($id) {
- global $db;
- $query = "SELECT * FROM tournaments ";
- $query .= "WHERE id='" . $id . "'";
- $result = mysqli_query($db, $query);
- $tournament = mysqli_fetch_assoc($result);
- mysqli_free_result($result);
- return $team;
- }
- function find_all_tournaments() {
- global $db;
- $query = "SELECT * FROM tournaments ";
- $result = mysqli_query($db, $query);
- return $result;
- }
- function insert_officer($id) {
- global $db;
- $query = "INSERT INTO officers ";
- $qurey .= "(user_id) ";
- $query .= "VALUES (";
- $query .= "'" .$id . "'";
- $query .= ")";
- $result = mysqli_query($db, $query);
- return $result;
- }
- function find_single_officer($id) {
- global $db;
- $query = "SELECT * FROM officers ";
- $query .= "WHERE user_id='". $id ."'";
- $result = mysqli_query($db, $query);
- $officer = mysqli_fetch_assoc($result);
- mysqli_free_result($result);
- return $officer;
- }
- function find_all_officers() {
- global $db;
- $query = "SELECT * FROM officers ";
- $result = mysqli_query($db, $query);
- return $result;
- }
- function find_single_match($id) {
- global $db;
- $query = "SELECT * FROM matches ";
- $query .= "WHERE id='" . $id . "'";
- $result = mysqli_query($db, $query);
- $match = mysqli_fetch_assoc($result);
- mysqli_free_result($result);
- return $match;
- }
- function insert_match($match_array) {
- global $db;
- /*
- $errors = validate_user($user_array);
- if (!empty($errors)) {
- return $errors;
- }
- */
- $query = "INSERT INTO matches ";
- $query .= "(tournament_id, player1_id, player2_id, date_played) ";
- $query .= "VALUES (";
- $query .= "'" . $user_array['tournament_id'] . "',";
- $query .= "'" . $user_array['player1_id'] . "',";
- $query .= "'" . $user_array['player2_id'] . "',";
- $query .= "'" . $user_array['dated_played'] . "'";
- $query .= ")";
- $result = mysqli_query($db, $query);
- // INSERT succeeded
- if ($result) {
- return true;
- } else {
- // INSERT failed
- echo mysqli_error($db);
- db_disconnect($db);
- exit;
- }
- }
- function find_all_matches() {
- global $db;
- $query = "SELECT * FROM MATCHES ";
- $result = mysqli_query($db, $query);
- return $result;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement