0

I'm trying to build a login form that will redirect me to an interface that allows users to eventually insert and delete data from a database, but I'm having trouble with the login itself, in the sense that, when I'm submitting the email and password I'm redirected to a page that shows the php code. The login credentials are stored in a database called test and in a table called users.Also I'm using xampp. Here is the HTML login form:

<!DOCTYPE html>
<html>
<head>
    <title>login Page</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
    <div class="container vh-100">
        <div class="row justify-content-center h-100">
            <div class="card w-25 my-auto shadow">
                <div class="card-header text-center bg-primary text-white">
                    <h2>Login</h2>
                </div>
                <div class="card-body">
                    <form action="login.php" method="post">
                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" id="email" class="form-control" name="email" />
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" id="password" class="form-control" name="password" />
                        </div>
                        <input type="submit" class="btn btn-primary w-100" value="Login" name="">
                    </form>
                </div>
                <div class="card-footer text-right">
                    <small>&copy; Coman Emanuel</small>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

And here is the php code:

<?php
$email =$_POST['email'];
$password= $_POST['password'];

// Database connection here
$con = new mysqli("localhost","root","","test");
if($con->connect_error){
    die("Failed to connect : ".$con->connect_error);
} 
else{
    $stmt = $con->prepare("select * from users where Email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt_result=$stmt->get_result();
    if($stmt_result->num_rows > 0) 
    {
        $data=$stmt_result->fetch_assoc();
        if($data['password']===$password)
            {
                echo "<h2>Login Successfully</h2>";
            }
            else
                {
                    echo "<h2>Invalid email or password</h2>";
                }
    }
    else
        {
            echo "<h2>Invalid Email or password</h2>";
        }
    }
}
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • what do you mean, "shows the PHP code"? Do you have PHP installed? – Your Common Sense May 17 '22 at 19:52
  • I mean, after submitting the credentials, I'm redirected to a page where it outputs the entire php code instead of "Login succesfully". Also, I'm using xampp as I specified, am I supposed to install something else? – Emanuel Coman May 17 '22 at 19:55
  • no, but you are supposed to turn that xampp stuff on and navigate to your page using a web browser. Looks like you need to consult that xampp manual in order how to use it – Your Common Sense May 17 '22 at 19:57
  • Both the Apache server and the mySQL are turned on, so I'm not sure I follow what you're trying to tell me – Emanuel Coman May 17 '22 at 19:59
  • They may be turned on, but you may not be using it. Make sure the URL in the browser starts with `http`, not `file` – aynber May 17 '22 at 19:59
  • This may sound silly, but how is the page supposed to start with http when it runs on a local server? – Emanuel Coman May 17 '22 at 20:02

0 Answers0