PHP:
session_start();
// connect to database
$db = mysqli_connect('localhost', 'user', 'password', 'db');
if($db === false){
die("error ".mysqli_connect_error());
}
// variable declaration
$username = "";
$email = "";
$sposo = "";//sposo
$sposa = "";//sposa
$errors = array();
// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
register();
}
// REGISTER USER
function register(){
// call these variables with the global keyword to make them available in function
global $db, $errors, $username, $email, $sposo, $sposa;//sposo
// receive all input values from the form. Call the e() function
// defined below to escape form values
$username = e($_POST['username']);
$email = e($_POST['email']);
$password_1 = e($_POST['password_1']);
$password_2 = e($_POST['password_2']);
$sposo = e($_POST['sposo']);
$sposa = e($_POST['sposa']);
// form validation: ensure that the form is correctly filled
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($sposo)) {
array_push($errors, "nome is required");
}
if (empty($password_1)) {
array_push($errors, "Password is required");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
if (isset($_POST['user_type'])) {
$user_type = e($_POST['user_type']);
$query = "INSERT INTO `users`(`username`, `email`, `password`, `user_type`, `sposo`, `sposa`) VALUES
('$username', '$email', '$password', '$user_type', '$sposo', '$sposa')";
mysqli_query($db, $query);
$_SESSION['success'] = "Account creato!";
header('location: admin.php');
}else{
$query = "INSERT INTO `users`(`username`, `email`, `password`, `user_type`, `sposo`, `sposa`) VALUES
('$username', '$email', '$password', '$user_type', '$sposo', '$sposa')";
mysqli_query($db, $query);
// get id of the created user
$logged_in_user_id = mysqli_insert_id($db);
$_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
$_SESSION['success'] = "Loggato";
header('location: index.php');
}
}
}