I already create the PHP login form. this login form uses PDO and connects with SQL server 2016. The problem is, it always said that email or password is invalid although I enter correctly. Just ignore about the password. I know about the encrypt. I will encrypt it after this.
Below is my current code:
<?php
session_start();
require_once 'configPDO.php';
if(ISSET($_POST['login'])){
if($_POST['Email'] != "" || $_POST['Pwd'] != ""){
$Email = $_POST['Email'];
$Pwd= $_POST['Pwd'];
$sql = "SELECT * FROM staff WHERE Email='?' AND Pwd='?'";
$query = $conn->prepare($sql);
$query->execute(array($Email,$Pwd));
$row = $query->rowCount();
$fetch = $query->fetch();
if($row > 0) {
$_SESSION['user'] = $fetch['Email'];
header("location: home.php");
} else{
echo "
<script>alert('Invalid Email or password')</script>
<script>window.location = 'index.php'</script>
";
}
}else{
echo "
<script>alert('Please complete the required field!')</script>
<script>window.location = 'index.php'</script>
";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - PDO Login and Registration</h3>
<hr style="border-top:1px dotted #ccc;"/>
<div class="col-md-2"></div>
<div class="col-md-8">
<form action="" method="POST">
<h4 class="text-success">Login here...</h4>
<hr style="border-top:1px groovy #000;">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="Email" />
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="Pwd" />
</div>
<br />
<div class="form-group">
<button class="btn btn-primary form-control" name="login">Login</button>
</div>
<a href="registration.php">Registration</a>
</form>
</div>
</div>
</body>
</html>
Can I know what's wrong with my code?
In your form your password field is named Role_ID
but it should be Pwd
.
Change password input to:
<input type="password" class="form-control" name="Pwd" />
You are not passing the password
<div class="form-group">
<label>Password</label>
<input type="text" class="form-control" **name="Role_ID"** />
</div>
Change the code
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="Pwd" />
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.