skip to Main Content

i need some help with my code, when i attempt to login it will not provide any error or any problem it will just not send me to the page it’s supposed to after a successful login. This is the code:

<?php
session_start();
include 'conn.php';

// User Authentication
if (isset($_POST['submit'])) {
    $username = $_POST['user'];
    $password = $_POST['pass'];


// Retrieve user information from the database
$stmt = $conn->prepare("SELECT id_user, password, pin FROM users WHERE username = ?");
$stmt->bind_param("s", $inputUsername);

if ($stmt->execute()) {
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $stmt->bind_result($userId, $hashedPassword, $hashedPin);
        $stmt->fetch();

        // Verify the entered password
        if (password_verify($inputPassword, $hashedPassword)) {
            // Password is correct, proceed with authentication

            // Check if the user has a PIN
            if (!empty($hashedPin)) {
                // User has a PIN, ask for PIN
                $_SESSION['user_id'] = $userId;
                $stmt->close(); // Close the statement before redirecting
                header("Location: enter_pin.php");
                exit();
            } else {
                // User doesn't have a PIN, direct to menu
                $_SESSION['user_id'] = $userId;
                $stmt->close(); // Close the statement before redirecting
                header("Location: menu.php");
                exit();
            }
        } else {
            echo "<div class='alert alert-danger'>Password is incorrect</div>";
        }
    } else {
        echo "<div class='alert alert-danger'>Username Not Found</div>";
        exit();
    }
} else {
    // Error in executing the statement
    die('Error: ' . $stmt->error);
}
}

// HTML Form for User Login
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login Page</title>
    <link rel="stylesheet" type="text/css" href="login-style.css">
</head>
<body>
    <h1>Login</h1>
    <?php
    // Display error message if present in the URL
    if (isset($_GET['error'])) {
        echo "<p>{$_GET['error']}</p>";
    }
    ?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" required><br><br>

        <label for="password">Password:</label>
        <input type="password" name="password" id="password" required><br><br>

        <input type="submit" value="Login">
    </form>
</body>
</html>

I have attempted to change my database and even the login code to others i have found online to see if the code is the problem nut the same issue persists. I’m using xampp and phpmyadmin for my test. ill provide a screenshot of my db for referance and the table. Sorry for my bad english it’s not my main language. db table db

2

Answers


  1. Chosen as BEST ANSWER

    It appears it was a problem in my menu.php, my login form sends the lodein user to the menu but the menu was put in a wrong code. The previous code was:

    <?php
    // Start or resume the session
    session_start();
    
    // Check if the user is authenticated
    if (isset($_SESSION['id_user'])) {
        $userID = $_SESSION['id_user']; // User is logged in
    } else {
        // Redirect the user to the login page if not authenticated
        header("Location: login.php");
        exit;
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Restaurant Home</title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="https://kit.fontawesome.com/a36d8a820b.js" crossorigin="anonymous"></script>
    </head>
    <body>
    <nav>
        <img src="imgchill_logo.svg" alt="Restaurant Logo">
        <i class="fas fa-bars" id="menu-icon" tabindex="0"></i>
        <ul class="nav-dropdown">
            <li><a href="index.php">Home</a></li>
            <li><a href="menu.php">Menu</a></li>
            <li><a href="contact.php">Contact</a></li>
            <li><a href="login.php">Login</a></li>
            <li><a href="register.php">Register</a></li>
        </ul>
    </nav>
    <h1>Welcome to Our Restaurant</h1>
    
    <h2>Menu</h2>
    <form method="post" action="order_processing.php">
        <ul class="menu-grid">
            <?php
            // Create a connection
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "chill_restaurant";
    
            $conn = new mysqli($servername, $username, $password, $dbname);
    
            // Check the connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
    
            // Fetch and display menu items from the database
            $sql = "SELECT id_menu, item_name, item_description, price FROM menu"; // Include 'id_menu' in the SQL query
            $result = $conn->query($sql);
    
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $menuItemID = $row['id_menu']; // Get the ID of the menu item
                    $imageFileName = str_replace(' ', '_', strtolower($row['name'])) . '.svg';
    
                    echo "<li class='menu-item'>";
                    echo "<img src='img/$imageFileName' alt='{$row['name']}' width='100' height='100'>";
                    echo "<h3>{$row['name']}</h3>";
                    echo "<p>{$row['description']}</p>";
                    echo "<p>$ {$row['price']}</p>";
                    echo "<label><input type='checkbox' name='selected_items[]' value='$menuItemID'> Select</label>"; // Use $menuItemID as the value
                    echo "</li>";
                }
            }
            ?>
        </ul>
    
        <h2>Order Online</h2>
        <label for="order_type">Order Type:</label>
        <select name="order_type" id="order_type">
            <option value="delivery">Delivery</option>
            <option value="pickup">Pick-up</option>
            <option value="dine-in">Dine-in</option>
        </select>
    
        <input type="submit" value="Place Order">
    </form>
    <script src="script.js"></script>
    </body>
    </html>
    

    the code used now is:

    <?php
    // Start or resume the session
    session_start();
    
    include 'conn.php';
    
    // Check if the user is authenticated
    if (isset($_SESSION['user_id'])) {
        $userID = $_SESSION['user_id']; // Use the correct session variable name
    } else {
        // Redirect the user to the login page if not authenticated
        header("Location: login.php");
        exit;
    }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Restaurant Home</title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="https://kit.fontawesome.com/a36d8a820b.js" crossorigin="anonymous"></script>
    </head>
    <body>
    <nav>
        <img src="imgchill_logo.svg" alt="Restaurant Logo">
        <i class="fas fa-bars" id="menu-icon" tabindex="0"></i>
        <ul class="nav-dropdown">
            <li><a href="index.php">Home</a></li>
            <li><a href="menu.php">Menu</a></li>
            <li><a href="contact.php">Contact</a></li>
            <li><a href="login.php">Login</a></li>
            <li><a href="register.php">Register</a></li>
        </ul>
    </nav>
    <h1>Welcome to Our Restaurant</h1>
    
    <h2>Menu</h2>
    <form method="post" action="order_processing.php">
        <ul class="menu-grid">
            <?php
            // Create a connection
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "chill_restaurant";
    
            $conn = new mysqli($servername, $username, $password, $dbname);
    
            // Check the connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
    
            // Fetch and display menu items from the database
            $sql = "SELECT id_menu, item_name, item_description, price FROM menu"; // Include 'id_menu' in the SQL query
            $result = $conn->query($sql);
    
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $menuItemID = $row['id_menu']; // Get the ID of the menu item
                    $imageFileName = str_replace(' ', '_', strtolower($row['item_name'])) . '.svg'; // Use 'item_name' instead of 'name'
            
                    echo "<li class='menu-item'>";
                    echo "<img src='img/$imageFileName' alt='{$row['item_name']}' width='100' height='100'>";
                    echo "<h3>{$row['item_name']}</h3>"; // Use 'item_name' instead of 'name'
                    echo "<p>{$row['item_description']}</p>"; // Use 'item_description' instead of 'description'
                    echo "<p>$ {$row['price']}</p>";
                    echo "<label><input type='checkbox' name='selected_items[]' value='$menuItemID'> Select</label>";
                    echo "</li>";
                }
            }
            ?>
        </ul>
    
        <h2>Order Online</h2>
        <label for="order_type">Order Type:</label>
        <select name="order_type" id="order_type">
            <option value="delivery">Delivery</option>
            <option value="pickup">Pick-up</option>
            <option value="dine-in">Dine-in</option>
        </select>
    
        <input type="submit" value="Place Order">
    </form>
    <script src="script.js"></script>
    </body>
    </html>
    

  2. There are a few problems here.

    You are using $username and $password to get the user inputs, but then you are binding $inputUsername and $inputPassword which are not defined.

    Secondly, your HTML form’s input names are username and password, but in your PHP you are using $_POST['user'] and $_POST['pass'].

    Final note: Since you are using isset($_POST['submit']), you need to set the name of your submit button: name="submit", otherwise isset($_POST['submit']) will never be true.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search