down2323 2014-12-10 21:27
浏览 33
已采纳

通过Errno 1062显示特定错误消息

I have a class that inserts the user from a registration form into a database. I am inserting 4 things: username, password, salt and an email. I am using errno 1062 to check for duplicate entries. But I need to check for specific duplicate entries. For example I have this logic that checks for errno 1062 but it's really not specific to the username being checked, it's just checking for an errno and assuming it's the username that's duplicated.

    if ($stmt->affected_rows == 1) {
        $this->success = "$this->username has registered.  You can now log in.";
    } elseif ($stmt->errno == 1062) {
        $this->errors[] = "$this->username is already in use.  Please chose another.";
    } else {
        $this->errors[] = 'Sorry, there was an issue with the database.';
    }

My question is, how could I use errno 1062 to check for both username, email or both? Seems like errno 1062 just checks for ANY duplicate entries.

  • 写回答

1条回答 默认 最新

  • duandang6111 2014-12-14 13:10
    关注

    errno only returns an integer, with no other information about what error occurred, but you should be able to use error for more information.

    According to the manual, if you have an errno 1062, error should be a string in this format:

    Duplicate entry '%s' for key '%s'

    You can parse that string to get the data you want using normal PHP string functions. If you have more than one error it will only 'complain' about the first one it finds (at least on my servers), so be aware you might end up asking the user to fix one thing, then coming back to correct another, etc. The only way I know of to improve that would be to run separate simple queries:

    SELECT COUNT(*) FROM table WHERE email = 'test@email.com'
    

    P.S. Do you really want to block duplicate passwords?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器