【问题标题】:Check if email exist in database (PDO) [closed]检查数据库中是否存在电子邮件(PDO)[关闭]
【发布时间】:2017-09-28 05:49:38
【问题描述】:

当用户在表单中填写他们的电子邮件地址时,我会检查该电子邮件地址是否存在于数据库中。

为什么代码不起作用?

这是我的代码:

php

<?php

session_start();

require 'database.php';

if(!empty($_POST['email'])):

  $records = $conn->prepare('SELECT id,email,password FROM users WHERE email = :email');
  $records->bindParam(':email', $_POST['email']);
  $records->execute();
  $results = $records->fetch(PDO::FETCH_ASSOC);

  echo 'Working';

    if(isset($_REQUEST['email'])){

    $email_to = $_REQUEST['email'];
    $email_subject = "Forgotten password";
    $email_from = "password@scoreplay.net";


    $email_message = $results['password'];

    $headers = 'From: ' .$email_from . "\r\n";
    @mail($email_to, $email_subject, $email_message, $headers);

    #header("Location: /success.php");

  exit();

}

endif;

?>

【问题讨论】:

  • 如何处理什么
  • $email = "???";
  • 您是在登录用户还是只是检查电子邮件是否存在?
  • @chris85 我只是在检查邮件是否存在。
  • 所以不需要password_verify($_POST['password'], $results['password'])。只需检查您是否收到退货,php.net/manual/en/pdostatement.rowcount.php

标签: php mysql email pdo


【解决方案1】:

你必须回显结果才能看到像这样的 if/else 条件

session_start();

require 'database.php';

if(!empty($_POST['email']) ):

  $records = $conn->prepare('SELECT id,email,password FROM users WHERE email = :email');
  $records->bindParam(':email', $email);
  $records->execute();
  $results = $records->fetch(PDO::FETCH_ASSOC);

  $message = '';

  if(count($results) > 0 && password_verify($_POST['password'], $results['password']) ){

    $message = 'Success!';

  } else {
    $message = 'Sorry, those credentials do not match';
  }
  // Now, you just have to add 
 echo $message;  
endif;

【讨论】:

  • $email 中也有一个值会很有用!
  • @RiggsFolly 搞笑;我不认为这个答案被接受,除非 OP 跑起来或者它没有解决问题。
  • @Fred-ii- 当然不是我的紫外线
猜你喜欢
  • 1970-01-01
  • 2012-03-13
  • 2014-03-15
  • 2014-05-10
  • 2021-04-24
  • 2023-02-03
  • 2019-06-29
  • 2019-09-17
相关资源
最近更新 更多