Register
connect_errno) {
echo "
MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}
"; exit(); } // Required field names $required = array('username', 'first_name', 'last_name', 'password', 'email', 'phone', 'address'); // Loop over field names, make sure each one exists and is not empty $error = false; foreach($required as $field) { if (empty($_POST[$field])) { $error = true; } } //check if everything is set if ($error) { redirect_to("register.php?msg=All fields are required"); } else { //success # prepare data for insertion $username = test_input($_POST['username']); $first_name = test_input($_POST['first_name']); $last_name = test_input($_POST['last_name']); $userpassword = test_input($_POST['password']); $email = test_input($_POST['email']); $phone = test_input($_POST['phone']); $address = test_input($_POST['address']); //when a user first registers, his/her userlvl will be set to "simple". if he chooses to add his car later on, his userlvl will be updated to "full" $userlvl = "simple"; //hash the password $password = password_hash($userpassword, PASSWORD_DEFAULT); # check if username and email exist else insert // u = username, e = emai, ue = both username and email already exists $exists = ""; $result = $mysqli->query("SELECT username from users WHERE username = '{$username}' LIMIT 1"); if ($result->num_rows == 1) { $exists .= "u"; } $result = $mysqli->query("SELECT email from users WHERE email = '{$email}' LIMIT 1"); if ($result->num_rows == 1) { $exists .= "e"; } if ($exists == "u") echo "Error: Username already exists!
"; else if ($exists == "e") echo "Error: Email already exists!
"; else if ($exists == "ue") echo "Error: Username and Email already exists!
"; else { # insert data into mysql database $sql = "INSERT INTO users (id, username, first_name, last_name, password, email , phone , address , user_lvl) VALUES (NULL, '$username', '$first_name', '$last_name', '$password', '$email' , '$phone' , '$address' , '$userlvl')"; if ($mysqli->query($sql)) { redirect_to("login.php?msg=Registered successfully"); } else { echo "MySQL error no {$mysqli->errno} : {$mysqli->error}
"; exit(); } } } } //end of check //sanitize data function test_input($data) { $data = trim($data); $data = htmlspecialchars(addslashes($data)); return $data; } //message if(isset($_GET['msg'])) { echo "".$_GET['msg']."
"; } ?>