【问题标题】:Where should I add the code so that it checks whether email exists or not?我应该在哪里添加代码以检查电子邮件是否存在?
【发布时间】:2020-08-28 13:39:28
【问题描述】:

我应该在哪里添加代码以检查电子邮件是否存在?我已经在一个数组中插入了我的所有详细信息我应该如何调用特定属性以及我应该在哪里实现它以便它检查我的数据库中是否存在多于一封电子邮件?

<?php
        $connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
        $message = '';
        if(isset($_POST["email"]))
        {
         sleep(3); 
         $query = "
         INSERT INTO tbl_login 
         (first_name, last_name, gender, email, password, address, mobile_no) VALUES 
         (:first_name, :last_name, :gender, :email, :password, :address, :mobile_no)
         ";
         $password_hash = password_hash($_POST["password"], PASSWORD_DEFAULT); 
         
         $user_data = array(
          ':first_name'  => $_POST["first_name"],
          ':last_name'  => $_POST["last_name"],
          ':gender'   => $_POST["gender"],
          ':email'   => $_POST["email"],
          ':password'   => $password_hash,
          ':address'   => $_POST["address"],
          ':mobile_no'  => $_POST["mobile_no"]
        );
         $statement = $connect->prepare($query); 
         if($statement->execute($user_data)) 
         {
          $message = '
          <div class="alert alert-success">
          Registration Completed Successfully
          </div>
          ';
          header("location:login.php");
        }
        else
        {
          $message = '
          <div class="alert alert-success">
          There is an error in Registration
          </div>
          ';
        }
        }
        ?>

【问题讨论】:

    标签: php html css mysql


    【解决方案1】:

    你可以编写一个 if 语句来执行一个查询并存储 将结果值传递给变量。现在,如果该变量为空,则表示数据库尚未注册相同的电子邮件。

    $sql = 'SELECT email from tbl_login WHERE email=:email' 
    $params = [
        :email => $_POST['email']
    ] ;
    
    // Now the if statement which checks if the entered email already exists in the DB
    $stmt = $connect->prepare($sql)
    $stmt->execute($params) 
    if(!$temp = $stmt->fetc()) 
    { // Email doesn't exist in the DB, procees to insert it} 
    else
    { // Email already exists in the DB} 
    

    【讨论】:

    • 如果用户 B 在用户 A 正在检查时插入怎么办?
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 2022-01-20
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多