gpt4 book ai didi

PHP SQL 选择哪里和

转载 作者:行者123 更新时间:2023-11-29 15:26:44 26 4
gpt4 key购买 nike

我不想在 MySQL 中保存双用户名和电子邮件,但现在这段代码不起作用,问题是什么?

我该怎么做?

function isUserExist($email,$userName){
global $connect,$tbl_users;
$email = sanitize($email);
$userName = sanitize($userName);
$sql = ("SELECT `email` FROM `$tbl_users` WHERE `email`=? AND `user_name`=?");
$result = $connect->prepare($sql);
$result->bindValue(1,$email);
$result->bindValue(2,$userName);
$result->execute();
if ($result->rowCount()>=1){
return $result;
}

return false;
}




<?php
$query = null;
$query_exist = null;

if (isset($_POST['btn_submit'])) {
$query_exist = isUserExist($_POST['email'],$_POST['userName']);
if ($query_exist){
echo 'A user with this email has already registered with the site to change your email and try again';
}
else {
$query = createUser($_POST['firstName'], $_POST['lastName'],
$_POST['userName'], $_POST['password'], $_POST['email'],
$_POST['mobile'], $_POST['role']);
}
}

最佳答案

您应该分别检查电子邮件和用户名,您现在使用 AND 子句,因此返回 true 的唯一方法是用户名和电子邮件在行中匹配,但您不不想这样!

尝试切换到OR。这样,即使用户不匹配但电子邮件匹配,它也会返回 true,反之亦然。

您还可以稍微整理一下代码以摆脱这些全局变量:

function isUserExist($email,$userName, $connect, $tbl_users)
{
$email = sanitize($email);
$userName = sanitize($userName);
$sql = "SELECT `email` FROM `$tbl_users` WHERE `email`=? AND `user_name`=?";
$result = $connect->prepare($sql);
$result->bindValue(1,$email);
$result->bindValue(2,$userName);
$result->execute();

return $result->rowCount() >= 1;
}

关于PHP SQL 选择哪里和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com