gpt4 book ai didi

php - RedBean findOne 失败?

转载 作者:行者123 更新时间:2023-11-29 03:02:22 25 4
gpt4 key购买 nike

我正在使用 RedBean PHP ORM 来尝试注册/获取用户。也就是说,给定一个逗号分隔列表,我做

 $emails = explode(',', trim($app->request->post('emails')));

然后将每封邮件传递给这个函数

function registerOrGetUser($email)
{
echo("R or G " . $email . "<br>");
$user = R::findOne('user', ' email = ? ', array($email));
if(!$user)
{
echo("Couldn't find user " . $email . ", creating new user.<br>");
//user does not exist, register them
$user = R::dispense('user');
$password = $random = substr(md5(rand()),0,8);
$user->email = $email;
$user->password = md5($password);
$user->role = 0;
R::store($user);
mail($user->email, "Welcome to imgstat!", "Welcome to imgstat, " . $user->email . "! An account has been created for you. Please sign in with this email address, and the following password: " . $password);
}
return $user;
}

注意调试的 echo 语句。我遇到的问题是它有时只能检测到用户是否已经存在。也就是说,如果我尝试添加

 test@test.test

它获取用户(并且不注册新用户)。但是,如果我尝试

 test@test.test, test@test.test

它在数据库中创建了第二个用户——据我所知,使用完全相同的电子邮件!从那时起,我可以根据需要添加任意数量的 test@test.tests - 他们将始终获得用户并且不会创建重复项。但出于某种原因,如果电子邮件不是逗号分隔列表中的唯一项目,则始终会创建第一个副本。

有什么想法吗?

最佳答案

注意空白。您的示例 test@test.test, test@test.test, test@test.test 完全符合您描述的问题。通过用 ',' 展开该字符串,您将得到:

Array
(
[0] => test@test.test
[1] => test@test.test
[2] => test@test.test
)

这就是为什么在使用邮件中的空格创建第二个用户后成功并从那时起被正确找到的原因。要防止出现空白,只需使用 registerOrGetUser(trim($email))

调用您的函数

关于php - RedBean findOne 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199139/

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