错误页面抛出异常

时间:2021-07-10 19:41:32

标签: mysql pdo

我正在制作一个登录系统并希望在注册表单上给出一个错误,但错误显示在它发布到的 registration.php 页面上。我希望它是一个错误,即登录表单上显示的电子邮件或用户名已存在。我已经尝试设置标头而不是抛出异常,但是重定向后我无法在注册表单上设置错误消息。

注册表格.html

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="style2.css">
    <link rel="stylesheet" href="style.css">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="form-background"></div>
    
    <form name="reg" id="reg-form"  method="POST" action="registration.php">
        <p id="title" colspan="2" style="padding:20px;">Registration Page</p>
        </tr>
<div class="username-input-wrapper">
    <i style="font-size:24px" class="fa fa-user-o" aria-hidden="true"></i>
        <tr>
        <td></td><td><input type="text" class="username-input" placeholder="username" name="username" required></td></tr></div>        
        <div class="email-input-wrapper">
            <i style="font-size:24px" class="fa">&#xf003;</i>
        <tr>
        <td></td><td><input type="text" id="email" class="email-input" placeholder="Email" name="email" required></td></tr></div>
        <div class="password-input-wrapper">
            <span class="material-icons-outlined" style="color: white; position: absolute;left: -50px; font-size:30px">
                lock
                </span>
        <input class="password-input" type="password" placeholder="password" name="password" required></div>
        <div class="button">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        <input id="Submit" class="submit" type="submit" name="register" value="Register">registreren</div>
        </form>
        <a href='login-form.html'>
     <div class="button2" ><span></span>
        <span></span>
        <span></span>
        <span></span>want to login?</div></a>
</body>
</html>

注册.php

<?php

session_start();

$pdo = new PDO('mysql:host=localhost;charset=utf8mb4;dbname=user', 'root', 'root', [
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_EMULATE_PREPARES => false
]);

function register(PDO $db, string $username, string $password, string $email)
{
    if (!$username || !$password || !$email) {
        throw new Exception('Username, password and email is required!');}
    
    
    $textemail = trim($_POST['email']);
$query = $db->prepare("Select * from accounts where email = :email");
$query->bindParam(':email', $textemail, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll();
    if( $result) {
        throw new Exception('email already exists');
        
    }
    $textusername = trim($_POST['username']);
$query = $db->prepare("Select * from accounts where username = :username");
$query->bindParam(':username', $textusername, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll();
    if( $result) {
        throw new Exception('username already exists');
    }
    $hash = password_hash($password, PASSWORD_DEFAULT);
     
    $stmt = $db->prepare('INSERT INTO accounts(Username, email, Hash, UserType) VALUES(?,?,?,?)');
    $stmt->execute([
        $username,
        $email,
        $hash,
        'user' // or admin
    ]);
    $count = $stmt->rowCount();
        if($count == 1){header('Location: login-form.html'); exit();}
}
if (isset($_POST['register'])) {
    register($pdo, $_POST['username'], $_POST['password'], $_POST['email']);
}


?>

0 个答案:

没有答案