douzhan2027 2014-01-18 02:03
浏览 20
已采纳

RedBean findOne失败了吗?

I am using the RedBean PHP ORM to try and register/get users. That is, given a comma separated list, I do

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

And then pass each email to this function

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;
}

Note the echo statement for debug. The issue I am having is that it is only sometimes able to detect whether or not the user already exists. That is, if I try to add

 test@test.test

it gets the user (and does not register a new one). However, if I try

 test@test.test, test@test.test

it creates a SECOND user in the database - with, as far as I can tell, the exact same email! From then on, I can add as many test@test.tests as I want - they will always get the user and not create duplicates. But for some reason, that first duplicate is ALWAYS created if the email is not the only item in the comma separated list.

Any ideas?

  • 写回答

2条回答 默认 最新

  • doy57007 2014-01-18 02:24
    关注

    Watch out for white-space. Your example test@test.test, test@test.test, test@test.test perfectly fits the problem you described. By exploding that string with ',' you'd get:

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

    That's why after creating the second user with the space in the mail succeeded and was correctly found from then on. To prevent white space, just call your function with registerOrGetUser(trim($email))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示