注册/登录表格:我的数据库中未插入任何数据

时间:2018-12-25 08:47:19

标签: mysql php-7.2

我正在尝试使用PHP(PHP 7.2.10)构建注册/登录表单,现在我只是想能够在数据库中插入新用户,并使用其中一个用户登录

<?php
//Connecion to the DB

try {
    $bdd = new PDO('mysql:host=localhost; dbname=dbtest; charset=utf8', 'root', '123456');
} catch (Exception $e) {
    echo "Error: ".$e;
}

//Registration form

//variables definition
if(isset($_POST['registration'])){
    $name = $_POST['name'];
    $first_name = $_POST['first_name'];
    $nickname = $_POST['nickname'];
    $age = $_POST['age'];
    $sex = $_POST['sex'];
    $passwd = $_POST['passwd'];
    $passwd2 = $_POST['passwd2'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $date = date('d/m/Y');

//checking mandatory fields are filled  
    if(!empty($nickname) AND !empty($email)){
        //checking entered email is valid
        if(filter_var($email, FILTER_VALIDATE_EMAIL)){
            //confirming password
            if($passwd == $passwd2){
                // checking the entered email address doesnt already exist in the DB
                $TestEmail = $bdd->query('SELECT id FROM test WHERE email = "'.$email.'"');
                if($TestEmail->rowCount() < 1 ){
                    $bdd->query('
                        INSERT INTO test 
                            (name, first_name, sex, nickname, age, address, email, passwd) 
                        VALUES 
                            ("'.$name.'", "'.$first_name.'", "'.$sex.'", "'.$nickname.'", "'.$age.'", "'.$address.'", "'.$email.'", "'.$passwd.'", "'.$date.'")
                    ');
                } else $return = "There is already a user registered with this email address";
            } else $return = "Entered passwords are not matching";
        } else $return = "Please enter a valid email address";
    } else $return = "At least one field is missing";
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
    <title>Registration/Login form</title>
</head>
<body>
    <?php if(isset ($_POST['registration']) AND isset($return)) echo $return; ?>
    <!-- Registration/login form -->
    <form action="" method="POST">
        <input type = "text" name = "name" placeholder = "Name"><br>
        <input type = "text" name = "first_name" placeholder = "First Name"><br>
        <input type = "text" name = "nickname" placeholder = "Nickname"><br>
        <input type = "text" name = "age" placeholder = "Age"><br>
        <select name = "sex" placeholder = "Sex"><br>
            <option value = "Male">Male</option>
            <option value = "Female">Female</option>
        </select><br>
        <input type = "text" name = "address" placeholder = "Address"><br>
        <input type = "email" name = "email" placeholder = "Email address"><br>
        <input type = "password" name = "passwd" placeholder = "Your Password"><br>
        <input type = "password" name = "passwd2" placeholder = "Confirm your Password"><br>

        <input type="submit" name = "registration" value="Register">
    </form>
    <hr>
    <?php if(isset ($_POST['login']) AND isset($return)) echo $return; ?>
    <form action="" method="POST">
        <input type = "email" name = "email" placeholder = "Email address"><br>
        <input type = "password" name = "mdp" placeholder = "Your Password"><br>

        <input type="submit" name = "login" value="Log in">
    </form>
</body>
</html>
  • 当尝试注册新用户时,我填写了所有字段,然后单击“注册”,但是什么也没有发生,它只是清空所有字段,并且没有数据插入到我的数据库中,尽管我得到了没有错误信息。

  • 尝试登录时,在表单上方出现以下错误:“注意:未定义的索引:第61行的C:\ wamp64 \ www \ Hakikar \ index.php中的passwd”,以及,尽管我同时填写了电子邮件和密码字段,但消息“至少缺少一个字段”

0 个答案:

没有答案