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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集