2011-06-14 94 views
6

我最近发现这个名为usercake(http://usercake.com/)的小用户类脚本,具有所有基本功能,似乎工作得很好。OOP PHP用户类(usercake)没有添加到数据库中

我的问题:第一个用户被添加到数据库罚款,但之后,它不工作。很明显,有一点点错误,我不明白(我不知道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 ; 
+0

错过了PK上的auto_increment? – Rufinus 2011-06-14 17:39:45

+9

什么是错误的代码,由全局变量开始,而不是使用pdo – dynamic 2011-06-14 17:40:24

+0

DB结构中存在自动增量。 @ yes123:你知道一个更好的PHP用户类,使用它不需要几个小时的时间使其工作? – Adam 2011-06-14 17:46:01

回答

0

你给名称为NOT NULL,并且在代码的Insert语句中没有发送名称值,所以mysql会抛出一个异常,表示Name不能为null,请检查一次。

+0

虽然名称不是NULL,但INSERT语句不包含它绝对没问题。 MySQL不会引发错误,它只会在'name'列中插入一个空值。如果它允许NULL,它会存储NULL。 – Abhay 2011-06-22 09:38:20

+0

Ahum。 MySQL会接受它的事实(带有警告,并不意味着这是一个很好的计划),除非您已经为该列传递了默认值。插入空字符串以绕过NOT NULL根本没有意义。 – 2011-06-22 09:43:46

2

对我来说,有两种可能性,为什么它不是第一个后进一步增加用户增加:

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

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

您是否使用您用于第二个帐户的第一个帐户的相同用户名或电子邮件地址?我相信你不能使用相同的用户名,但可能使用相同的电子邮件地址。 usercake类不允许相同的用户名或相同的电子邮件地址。

希望这会有所帮助。

2

我会做4件事与此uggly代码:

1)启用的error_reporting模式,这样就可以在案件STHG看到的东西发生:

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找到另一个更好的编码。

+0

感谢您的建议,我实际上找到了其他一些叫做“access_user”的类,它看起来好多了。到目前为止,我没有遇到任何问题。 – Adam 2011-06-23 04:03:38