prepare("SELECT user_id FROM users WHERE email = ? LIMIT 1");
$stmt_email->bind_param("s", $email);
$stmt_email->execute();
$stmt_email->store_result();
if ($stmt_email->num_rows > 0) {
// Pass error message in URL parameter
$error_message = urlencode('Email address is already registered.');
$stmt_email->close();
$conn->close();
header('Location: register?error=' . $error_message);
exit();
}
$stmt_email->close();
// Check if mobile number already exists (only if provided)
if (!empty($mobile)) {
$stmt_mobile = $conn->prepare("SELECT user_id FROM users WHERE mobile = ? LIMIT 1");
$stmt_mobile->bind_param("s", $mobile);
$stmt_mobile->execute();
$stmt_mobile->store_result();
if ($stmt_mobile->num_rows > 0) {
// Pass error message in URL parameter
$error_message = urlencode('Mobile number is already registered.');
$stmt_mobile->close();
$conn->close();
header('Location: register?error=' . $error_message);
exit();
}
$stmt_mobile->close();
}
// Hash the password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Insert the new user into the database
// Using 'fullname' to match database column name
$stmt_insert = $conn->prepare("INSERT INTO users (fullname, email, mobile, password) VALUES (?, ?, ?, ?)");
$stmt_insert->bind_param("ssss", $fullname, $email, $mobile, $hashed_password);
if ($stmt_insert->execute()) {
// Registration successful, pass success message in URL parameter and redirect to login page
$success_message = urlencode('Registration successful! You can now login.');
// Use HTML redirect for better mobile compatibility
echo '
Redirecting...
Registration successful! Redirecting to login...
If you are not redirected automatically, click here.