简体   繁体   English

检查用户是否存在AJAX和PHP

[英]Check if user exists with AJAX and PHP

I am trying to create a signup form that checks if the user exists in the database, I inserted a sample user and when I tried signing up with that user it didn't say its already been taken. 我正在尝试创建一个注册表单,检查用户是否存在于数据库中,我插入了一个示例用户,当我尝试注册该用户时,它没有说它已被采用。 What have I done wrong? 我做错了什么?

The JavaScript: JavaScript:

function formSubmit()
    {
    document.getElementById('email_valid').innerHTML = '';

    var temail=document.forms["signup_form"]["temail"].value.replace(/^\s+|\s+$/g, '');


      var atpos=temail.indexOf("@");
        var dotpos=temail.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=temail.length)
          {
          //alert("Not a valid e-mail address");
          setTimeout(function(){document.getElementById('email_valid').innerHTML = '<br/>Email must be valid...';},1000);
          var temailsub=0;
          }
          else
          {
          $.post('/resources/forms/signup/email.php',{email: temail}, function(data){
            document.getElementById('email_valid').innetHTML = data;
            if(data.exists){
                    document.getElementById('email_valid').innetHTML = '<br/>The email address you entered is already in use.';
                    var temailsub=0;
                }else{
                    var temailsub=1;
                }
            }, 'JSON');
          }
    if(temailsub==1e)
      {
        setTimeout(function(){document.getElementById("signup_form").submit();},1000);
      }
      else
      {

        return false;
      }
    }

The PHP file (email.php): PHP文件(email.php):

<?php
header('content-type: text/json');
require_once $_SERVER['DOCUMENT_ROOT']."/resources/settings.php";
$query = $pdo->prepare("SELECT * FROM users WHERE email=:email");
$query->execute(array(
    ":email"=> $_POST['email']
));
echo json_encode(array('exists' => $query->rowCount() > 0));
?>

I have checked and double checked the code, I still cannot see why its not detecting that the email has already been used... what do i need to do to fix this and avoid this in the future? 我已经检查并仔细检查了代码,我仍然无法理解为什么它没有检测到电子邮件已被使用...我需要做些什么来解决这个问题并在将来避免这种情况?

The problem is that PDOStatement::rowCount() returns the number of rows affected by the last SQL statement. 问题是PDOStatement :: rowCount()返回受最后一个SQL语句影响的行数。 You are performing a SELECT so this value will always be 0 . 您正在执行SELECT因此该值始终为0 SELECT does not affect any rows. SELECT不会影响任何行。 Instead you need to count the number of rows: 相反,您需要计算行数:

$query = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email=:email");
$query->execute(array(
    ":email"=> $_POST['email']
));
$rows = $query->fetchColumn();

echo json_encode(array('exists' => $rows);

Also from jtheman's comment above, you should replace innetHTML with innerHTML in your JavaScript. 同样来自jtheman上面的评论,你应该在JavaScript中用innerHTML替换innetHTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM