-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
58 lines (50 loc) · 1.91 KB
/
register.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
$servername = "localhost"; //
$username = "root"; // Replace with your MySQL username
$password = ""; // Replace with your MySQL password
$dbname = "irp"; // Replace with your MySQL database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$gender = $_POST["gender"];
$dob = $_POST["dob"];
// Prevent SQL injection
$name = mysqli_real_escape_string($conn, $name);
$username = mysqli_real_escape_string($conn, $username);
$email = mysqli_real_escape_string($conn, $email);
$password = mysqli_real_escape_string($conn, $password);
$gender = mysqli_real_escape_string($conn, $gender);
$dob = mysqli_real_escape_string($conn, $dob);
// Check if username or email already exists
$checkQuery = "SELECT * FROM users WHERE username='$username' OR email='$email'";
$checkResult = $conn->query($checkQuery);
if ($checkResult->num_rows > 0) {
// Username or email already exists
header("Location: register.php?error=UsernameOrEmailExists"); // Redirect back to registration page with error message
exit();
} else {
// Insert new user into database
$insertQuery = "INSERT INTO users (name, username, email, password, gender, dob)
VALUES ('$name', '$username', '$email', '$password', '$gender', '$dob')";
if ($conn->query($insertQuery) === TRUE) {
// Registration successful
session_start();
$_SESSION["username"] = $username;
header("Location: index.php"); // Redirect to profile page
exit();
} else {
// Error while inserting into database
echo "Error: " . $insertQuery . "<br>" . $conn->error;
}
}
}
$conn->close();
?>