Php 尝试查看电子邮件是否不是重复的
我试图验证数据库中是否已经存在电子邮件。 如果我尝试验证用户名,同样的系统工作得很好。 我正在使用AJAX和PHP。 这是获取$\u POST变量的文件Php 尝试查看电子邮件是否不是重复的,php,mysql,ajax,database,email,Php,Mysql,Ajax,Database,Email,我试图验证数据库中是否已经存在电子邮件。 如果我尝试验证用户名,同样的系统工作得很好。 我正在使用AJAX和PHP。 这是获取$\u POST变量的文件 <?php require_once 'Config.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $email = $_POST['email']; $password = $_POST['password']; if (!empty($password
<?php
require_once 'Config.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
$password = $_POST['password'];
if (!empty($password) and !empty($email)) {
$notEmpty = true;
include 'validate.php';
if($notEmpty == true and validateEmail($email) == true){
$password = md5($password);
$stmt = $link->prepare("INSERT INTO `Users`(`user_password`, `user_email`) VALUES (?,?)");
$stmt->bind_param("ss",$email,$password);
$stmt->execute();
}
}else{
$notEmpty == false;
}
}
?>
Js代码(ajax):
无论如何,它都会插入它,这里有什么问题?如果在执行准备好的语句后立即调用
num_rows()
,它通常会返回0,因为它无法知道结果集中有多少行,因为结果集尚未保存在内存中。必须首先调用缓冲结果,以便后续调用num\u rows()
将包含正确的结果
这在本手册底部的“用户说明”部分有解释。顺便说一句,当用户已经存在时,它仍然会插入它。1。为什么要给我们看两次
validateEmail
的代码?有区别吗validateEmail
是一个函数!!有没有调用该函数的代码?哦,对不起,我现在添加它。我所能想到的是include'../Backend/Config.php'代码>不创建$link
连接
function validateEmail($user_email){
include '../Backend/Config.php';
$sql = "SELECT `user_password`, `user_email` FROM `Users` WHERE `user_email` = ?";
$stmt = $link->prepare($sql);
$stmt->bind_param("s",$user_email);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$row = $result->fetch_assoc();
if ($result->num_rows > 0) {
echo 1;
return false;
}
else{
// echo json_encode(array('status' => 'OK'));
echo 0;
return true;
}
}
$('#form').submit(function(e) {
//Don't refresh the page
e.preventDefault();
//Collecting data for the server call
post_data = {
'email' : $('input[name=email]').val(),
'password': $('input[name=password]').val()
};
//AJAX server call to signup.php,which calls validate.php
$.ajax({
method: "POST",
url: "../Backend/signup.php",
data: post_data
})
//Server response and setting the input values to ""
.then(function( msg ) {
if(msg == 0){
console.log("Success, user data inserted. Code: " + msg);
}
//Failed
if(msg == 1){
console.log("Inserting failed. Error code:" + msg);
document.getElementById("error").innerHTML = "This email already exists.";
}
$('input[name=email]').val("");
$('input[name=password]').val("");
});
});