我要做的是验证电子邮件地址是否在数据库中。我CheckEmailAddress
检查电子邮件是否在数据库中并且它似乎有效(如果需要,重写它)。问题是当我从我的 JQuery 函数发布我的 JSON 电子邮件数据时,如果在数据库中输入了电子邮件,则它总是返回 true,而应该是 false。
查询:
$('#checkemail').click(function() {
$.post('http://' + location.host + '/buyme/include/getemailaddress.php',
{'email':'test012@yahoo.co.nz'}, function(res){
var obj = JSON.parse(res);
alert(obj)
});
});
PHP
<?PHP
require_once("membership.php");
$membership = new Membership();
$val = $_POST["email"];;
$result = $membership->CheckEmailAddress(trim($val));
echo json_encode($result); //output test@test.com
?>
function CheckEmailAddress($email_address) {
$connection = mysql_connect('localhost:3306','root','')or die('Error connecting');
mysql_select_db('buyme') or die('Connection not working properly');
$query = "SELECT email_address from users where email_address='$email_address'";
$result = mysql_query($query, $connection);
$row = mysql_fetch_assoc($result);
$emailInUse = 'false';
if(!$row || $row["email_address"] == '') {
$emailInUse = 'true';
}else{
$emailInUse = 'false';
}
return $emailInUse;
}
如果有什么需要更正,请更新我的代码,以便我测试谢谢
更新:
try {
$pdo = new PDO('mysql:dbname=buyme;host=localhost:3306', 'root', '',
$options = array (
PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION
));
$email_address = 'kirkdm021@yahoo.co.nz';
$stmt = $pdo->prepare("SELECT email_address
FROM users
WHERE email_address=:email_address");
$stmt->execute(array('email_address'=>$email_address));
$rows = $stmt->fetchObject();
echo empty($rows->email_address) ? "" : $rows->email_address; // $row["email_address"];
}catch (PDOException $e) {
die('error: ' . $e->getMessage());
}