I have created a form that can be navigated between login and register user. I created one table in database using mysql for admin with one record only and multiple records for user table, so if for example I followed the method where user select his role (admin/user) upon login, then it is also easy for new users when they register new account, to define themselves as admin upon registration, how to make the login process using 2 tables admin and users from the same login form?
login.php
<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");
if (isset($_POST['submit'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = md5($_POST['password']);
$user_type = $_POST['user_type'];
$select = "SELECT * FROM users where username='$username' and password='$password'";
$result = mysqli_query($conn, $select);
if (mysqli_num_rows($query_run) > 0) {
$row = mysqli_fetch_array($result);
if ($row['user_type'] == 'admin') {
$_SESSION['admin_name'] = $row['name'];
header('location:admin_page.php');
} else if ($row['user_type'] == 'user') {
$_SESSION['user_name'] = $row['name'];
header('location:user_page.php');
} else {
$error[] = 'incorrect email or password!';
}
}
};
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Login</title>
</head>
<body>
<div class="container">
<form action="" method="POST" class="login-email">
<?php
if (isset($error)) {
foreach ($error as $error) {
echo '<span class="error-msg">' . $error . '</span>';
};
};
?>
<p class="login-text" style="font-size: 2rem; font-weight: 800;">Login</p>
<div class="input-group">
<input type="text" name="username" value="" required>
</div>
<div class="input-group">
<input type="password" name="password" value="" required>
</div>
<div class="input-group">
<button name="submit" class="btn">Login</button>
</div>
<p class="login-register-text">Don't have an account? <a href="register.html">Register Here</a>.</p>
</form>
</div>
</body>
</html>
register.php
<?php
$conn = mysqli_connect("localhost", "root", "", "finalproject") or die ("<script>alert('Connection Failed.')</script>");
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
$phone_number = $_POST['phone_number'];
$work_duration = $_POST['work_duration'];
$ig_account = $_POST['ig_account'];
$state = $_POST["state"];
$postcode = $_POST['postcode'];
$city = $_POST['city'];
$gender = $_POST['gender'];
$age = $_POST['age'];
if ($password == $cpassword) {
$sql = "SELECT * FROM users WHERE email='$email'";
$result = mysqli_query($conn, $sql);
if (!$result->num_rows > 0) {
$sql = "INSERT INTO users VALUES ('$username','$full_name', '$email', '$password', '$phone_number', '$work_duration', '$ig_account', '$state', '$postcode', '$city', '$gender', '$age')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo "<script>alert('Wow! User Registration Completed.')</script> " ;
header("Location: successregister.html");
$username = "";
$full_name = "";
$email = "";
$_POST['password'] = "";
$_POST['cpassword'] = "";
$phone_number = "";
$work_duration = "";
$ig_account = "";
$state = "";
$postcode = "";
$city = "";
$gender = "";
$age = "";
}
else {
echo "<script>alert('Woops! Something Wrong Went.')</script>";
echo $wrong = mysqli_error($conn);
}
} else {
echo "<script>alert('Woops! Email Already Exists.')</script>";
echo $wrong = mysqli_error($conn);
}
} else {
echo "<script>alert('Password Not Matched.')</script>";
echo $wrong = mysqli_error($conn);
}
}
else
echo "<script>alert('No session exist or session is expired. Please log in again')</script>";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Registeration</title>
</head>
<body>
<div class="container">
<form action="process.php" method="POST" class="login-email">
<p class="login-text" style="font-size: 2rem; font-weight: 800;">Register</p>
<div class="input-group" style="margin-bottom: 40px;">
Username:<input type="text" name="username" value="" required></input>
</div>
<div class="input-group" style="margin-bottom: 40px;">Full Name:
<input type="text" name="full_name" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Email:
<input type="email" name="email" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Password:
<input type="password" name="password" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Confirm Password:
<input type="password" name="cpassword" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Age:
<input type="text" name="age" value="" required>
</div>
<div style="margin-top: 40px; margin-bottom: 20px;">Gender:
<label for="f-option" class="l-radio">
<input type="radio" name="gender" tabindex="1" value="Male" style="margin-left:10px;">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female" style="margin-left:20px;">
<span> Female</span>
</div>
<div class="input-group" style="margin-bottom: 40px;">Phone Number:
<input type="text" name="phone_number" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Work Duration:
<input type="text" name="work_duration" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">IG Account:
<input type="text" name="ig_account" value="" required>
</div>
<div class="form-group" style="margin-bottom: 10px;">
<label >State:</label>
<select name="state" class="form-control" required>
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>
</div>
<div class="input-group" style="margin-bottom: 40px;">Postcode:
<input type="text" name="postcode" value="" required>
</div>
<div class="input-group" style="margin-bottom: 50px;">City:
<input type="text" name="city" value="" required>
</div>
<div class="input-group" style="margin-top:40px;">
<button name="submit" class="btn">Register</button>
</div>
<p class="login-register-text">Have an account? <a href="login.php">Login Here</a>.</p>
</form>
</div>
</body>
</html> ?>