
PHP Login Form Using PDO Prepared Statement
Below is a tutorial on how to create a login form using PDO prepared statement, we created an HTML form for the user to input login information, connect to the database with required_once, use try and catch to get the error, query the database with PDO prepared statement and use if statement for validation of login information.
<?php require_once("pdo_db_conn.php");
if(isset($_POST['form_submit'])){
$email = $_POST['form_email'];
$password = $_POST['form_password'];
try{
$validation_query = $pdo->prepare("SELECT * FROM registration WHERE email = ? AND password = ?");
$validation_query->execute([$email,$password]);
if($validation_query->rowCount()>0){
echo "Email exist";
}else{
echo "Email doesn't";
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Form</title>
</head>
<body>
<form action="login.php" method="post">
<input type="email" name="form_email" required>
<input type="password" name="form_password" required>
<input type="submit" name="form_submit" value="Login">
</form>
</body>
</html>