This occurs when I reinstall XAMMP but I don’t think that is not the issue.
When I am trying to submit the form before it redirects to index page and logging the user in and starting a session but now after reinstalling XAMMP and recreate the database it is not working.
But there were changes I made upon creating the database I changed the value and type but I don’t think that is the issue here.
Also, the log-in function is not working either it gives the error handler wrong password even thou it is correct since I used the password verify for the password and hashed password.
I really appreciate your help I tried searching and can’t find the correct answer
Here is the code below for my controller.php :
// Check if there are any errors
if (count($errors) == 0) {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Insert the new user into the database, but do not log them in
if (isset($_POST['user_type'])) {
$usertype = $_POST['user_type'];
$query = "INSERT INTO user_table (username, email, user_type, password) VALUES
(?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->bind_param('ssss', $username, $email, $usertype, $hashed_password);
$stmt->execute();
header('Location: ./admin.php');
} else {
// Insert the new user into the database and log them in
$query = "INSERT INTO user_table (username, email, user_type, password) VALUES
(?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->bind_param('ssss', $username, $email, $usertype, $hashed_password);
$stmt->execute();
// Check the user's credentials
$query = $db->prepare("SELECT * FROM user_table WHERE username = ? AND password
= ?");
$query->bind_param('ss', $username, $hashed_password);
$query->execute();
$result = $query->get_result();
// If a matching record was found, log the user in
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
// Start a new session
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['user_type'] = $user['user_type'];
$_SESSION['success'] = "You are now logged in";
header('location:../index.php');
}
}
function loginuser() {
global $db, $username, $password, $errors;
// Prepare the SQL statement
$stmt = $db->prepare("SELECT * FROM user_table WHERE username = ?");
// Bind the username to the prepared statement
$stmt->bind_param("s", $username);
// Execute the statement
$stmt->execute();
// Get the result
$result = $stmt->get_result();
// Check if the username exists in the database
if ($result->num_rows === 0) {
// Show an error message if the username does not exist
$errors['inlgn'] = "Incorrect username or password";
} else {
// Get the user data
$user = $result->fetch_assoc();
// Get the hashed password from the database
$hashed_password = $user['password'];
// Verify the password using the password_verify() function
if (password_verify($password, $hashed_password)) {
// The password is correct, so log the user in
// Start a new session
session_start();
// Store the user's ID and username in the session
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['user_type'] = $user['user_type'];
// Set the success message
$_SESSION['success'] = "You are now logged in";
// Redirect the user to the homepage
header('Location: ./index.php');
} else {
// The password is incorrect, so show an error message
$errors['pwdm'] = "Wrong Password";
}
}
}
