gpt4 book ai didi

PHP 回调 MySQL 从不返回值

转载 作者:行者123 更新时间:2023-11-30 22:52:04 24 4
gpt4 key购买 nike

我有 2 种回调方法可用于验证用户输入,一种用于检查电子邮件是否存在于数据库中,另一种用于检查用户是否存在。我的电子邮件 one 工作正常,但用户名 one 永远不会正确执行(因为它永远不会返回用户)。

用户名回调代码

// username check in DB
private function username($username, $fieldname){
$username = mysql_real_escape_string ($username);
if(strlen ($username) > 3 && strlen ($username) <= 25){
$sql = "SELECT username FROM pre_reg WHERE username =:username UNION SELECT username FROM personal WHERE username =:username";
try{
$query = $this->pdo->prepare($sql);
$query->bindParam(':username', $username, PDO::PARAM_STR);
$query->execute();
$user = $query->fetch();
if($user){
$this->errors[] = 'Please try a different username!';
return FALSE;
}else{
$this->username = $username;
return TRUE;
}
}catch (PDOException $e){
$e->getMessage(); // Store to file
}
}
return FALSE;
}

检查电子邮件的相同代码运行良好

//email check
private function email($email, $fieldname){
$valid = filter_var($email, FILTER_VALIDATE_EMAIL);
if(!$valid){
$this->errors[] = "We find your entery to be an invalid email address!";
}
if($valid && strlen($email) <= 40){
$email = mysql_real_escape_string ($email);
$sql = "SELECT email FROM pre_reg WHERE email =:email UNION SELECT email FROM personal WHERE email=:email";
try{
$query = $this->pdo->prepare($sql);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->execute();
$user = $query->fetch();
if($user){
$this->errors[] = 'Please try a different email address!';
return FALSE;
}
}catch (PDOException $e){
echo $e->getMessage(); // Store to file
}

}
$this->email = $email;
return TRUE;
}

这就是我对它们的称呼

// Validate all the data 
public function validate($data, $rules){
$valid = TRUE;
foreach($rules as $fieldname => $rule){
$callbacks = explode('|', $rule);
foreach($callbacks as $callback){
$value = isset($data[$fieldname]) ? $data[$fieldname] : NULL;
if($this->$callback($value, $fieldname) == FALSE){
$valid = false;
}
}
}

最佳答案

我采取的解决步骤:

回应这个声明

catch (PDOException $e){
$e->getMessage(); // Store to file
}

它告诉我表中没有 COLUMN,一旦我返回并查看我的语句,我意识到我在这里调用了一个错误的表:

UNION SELECT username FROM personal WHERE username =:user"

我实际上是想调用:

UNION SELECT username FROM users WHERE username =:user"

我收到此错误的原因是因为我从电子邮件功能中复制粘贴了代码而忘记更改表格。下次记住这一点!

感谢您的帮助!

关于PHP 回调 MySQL 从不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27897870/

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