【问题标题】:OOP PHP user class (usercake) not adding to databaseOOP PHP用户类(usercake)未添加到数据库
【发布时间】:2011-09-14 21:55:51
【问题描述】:

我最近发现了这个名为 usercake (http://usercake.com/) 的小用户类脚本,它具有所有基本功能并且看起来运行良好。

我的问题:第一个用户可以正常添加到数据库中,但之后就无法正常工作了。显然,我没有弄清楚只是有些小问题(我不太了解 oop php)。没有错误发生(我可以看到),并且电子邮件被发送出去。

我在多个地方安装了它,命运相同。我想修复它,因为使用此脚本可以节省大量重新发明轮子的时间。

这是我的网址:http://rawcomposition.com/birding/loggedin/register.php 这是验证所有内容后调用的函数:

    public function userCakeAddUser()
{
    global $db,$emailActivation,$websiteUrl,$db_table_prefix;

    //Prevent this function being called if there were construction errors
    if($this->status)
    {
        //Construct a secure hash for the plain text password
        $secure_pass = generateHash($this->clean_password);

        //Construct a unique activation token
        $this->activation_token = generateActivationToken();

        //Do we need to send out an activation email?
        if($emailActivation)
        {
            //User must activate their account first
            $this->user_active = 0;

            $mail = new userCakeMail();

            //Build the activation message
            $activation_message = lang("ACTIVATION_MESSAGE",array($websiteUrl,$this->activation_token));

            //Define more if you want to build larger structures
            $hooks = array(
                "searchStrs" => array("#ACTIVATION-MESSAGE","#ACTIVATION-KEY","#USERNAME#"),
                "subjectStrs" => array($activation_message,$this->activation_token,$this->unclean_username)
            );

            /* Build the template - Optional, you can just use the sendMail function 
            Instead to pass a message. */
            if(!$mail->newTemplateMsg("new-registration.txt",$hooks))
            {
                $this->mail_failure = true;
            }
            else
            {
                //Send the mail. Specify users email here and subject. 
                //SendMail can have a third parementer for message if you do not wish to build a template.

                if(!$mail->sendMail($this->clean_email,"New User"))
                {
                    $this->mail_failure = true;
                }
            }
        }
        else
        {
            //Instant account activation
            $this->user_active = 1;
        }   


        if(!$this->mail_failure)
        {
                //Insert the user into the database providing no errors have been found.
                $sql = "INSERT INTO `".$db_table_prefix."Users` (
                        `Username`,
                        `Username_Clean`,
                        `Password`,
                        `Email`,
                        `ActivationToken`,
                        `LastActivationRequest`,
                        `LostPasswordRequest`, 
                        `Active`,
                        `Group_ID`,
                        `SignUpDate`,
                        `LastSignIn`
                        )
                        VALUES (
                        '".$db->sql_escape($this->unclean_username)."',
                        '".$db->sql_escape($this->clean_username)."',
                        '".$secure_pass."',
                        '".$db->sql_escape($this->clean_email)."',
                        '".$this->activation_token."',
                        '".time()."',
                        '0',
                        '".$this->user_active."',
                        '1',
                        '".time()."',
                        '0'
                        )";

            return $db->sql_query($sql);
        }
    }
}

这是表结构:

CREATE TABLE IF NOT EXISTS `userCake_Users` (
  `User_ID` int(11) NOT NULL AUTO_INCREMENT,
  `Username` varchar(150) NOT NULL,
  `Name` varchar(100) NOT NULL,
  `Username_Clean` varchar(150) NOT NULL,
  `Password` varchar(225) NOT NULL,
  `Email` varchar(150) NOT NULL,
  `ActivationToken` varchar(225) NOT NULL,
  `LastActivationRequest` int(11) NOT NULL,
  `LostPasswordRequest` int(1) NOT NULL DEFAULT '0',
  `Active` int(1) NOT NULL,
  `Group_ID` int(11) NOT NULL,
  `SignUpDate` int(11) NOT NULL,
  `LastSignIn` int(11) NOT NULL,
  PRIMARY KEY (`User_ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

【问题讨论】:

  • 错过了 PK 的自动增量?
  • 多么糟糕的代码,从全局变量开始而不使用 pdo
  • 数据库结构中存在自动增量。 @yes123:你知道一个更好的 PHP 用户类来使用它不需要花费数小时的时间来使其工作吗?
  • 检查models/settings.php中的数据库连接细节,尤其是数据库主机。也尝试添加 ini_set('display_errors', E_ALL);在脚本的开头,您可能会看到一些错误/警告。
  • 设置正确,我尝试在注册页面上显示所有错误代码,但没有成功。它添加了第一个用户,但之后没有添加任何内容..

标签: php mysql database oop usercake


【解决方案1】:

对我来说,添加第一个用户后没有添加更多用户的原因有两种:

首先,在创建第一个用户后,以下用户帐户的$this->mail_failure 标志设置为 TRUE。但这种情况不太可能发生,因为它是为第一个用户成功运行的相同代码,因此没有理由让其他人的标志为 TRUE。

第二种可能性是$this->status 对于第二个用户帐户为 FALSE。如果为 false,则方法 userCakeAddUser() 不执行任何操作。此标志可能为假的原因是用户名或电子邮件地址已经存在。

您是否也在为第二个帐户使用与第一个帐户相同的用户名或电子邮件地址?我确定您不能使用相同的用户名,但可能使用相同的电子邮件地址。 usercake 类不允许使用相同的用户名或相同的电子邮件地址。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我会用这个丑陋的代码做 4 件事:

    1) 启用 error_reporting 模式,以便在发生某事时可以看到一些东西:

     error_reporting(E_ALL);  
    

    2) 测试这个 INSERT sql 直接进入 dB 以确保它正常工作,并验证这段代码。如果 sql INSERT 请求有效,则检查这些 SQL 请求的访问条件,如上面 Abhay 所说,

    3) 由于我们没有您的所有配置,猜谜游戏很困难。所以我建议你为 AI User_ID 添加一个 NULL 字段。

    $sql = "INSERT INTO `".$db_table_prefix."Users` (
                        `User_ID`, // Add this here
                        `Username`,
                        `Username_Clean`,
                        `Password`,
                        `Email`,
                        `ActivationToken`,
                        `LastActivationRequest`,
                        `LostPasswordRequest`, 
                        `Active`,
                        `Group_ID`,
                        `SignUpDate`,
                        `LastSignIn`
                        )
                        VALUES (
                        NULL,  // and that one
                        '".$db->sql_escape($this->unclean_username)."',
                        '".$db->sql_escape($this->clean_username)."',
                        '".$secure_pass."',
                        '".$db->sql_escape($this->clean_email)."',
                        '".$this->activation_token."',
                        '".time()."',
                        '0', // later, I would also try using an int for an int
                        '".$this->user_active."',
                        '1',
                        '".time()."',
                        '0'
                        )";
    

    4) 使用 OOP 和 PDO 找到另一个更好的编码。

    【讨论】:

    • 感谢您的建议,我实际上找到了其他一些名为“access_user”的类,它似乎要好得多。到目前为止,我还没有遇到任何问题。
    【解决方案3】:

    您将 Name 指定为 NOT NULL 并且在代码的 Insert 语句中没有发送 Name 值,因此 mysql 会抛出异常,说 Name 不能为 null,请检查一次。

    【讨论】:

    • 虽然 Name 不是 NULL,但 INSERT 语句不包含它是绝对可以的。 MySQL 不会抛出任何错误,它只会在name 列中插入一个空值。如果它允许 NULL,它会改为存储 NULL。
    • 嗯。 MySQL 将接受它(请注意)这一事实并不意味着它是一个好的计划,除非您为该列传递了默认值。插入一个空字符串只是为了绕过 NOT NULL 根本没有意义。
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多