2017-03-06 13 views
0

確認する前にユーザーの電話番号と国をデータベースに挿入するユーザー確認フォームを作成中です。しかし、最初の行を挿入した後、2番目の行は挿入されません。私の 'user' mysqlテーブルでは、私はAUTO_INCREMENTのプライマリキーとして 'id'を持っています。mysqlの最初の行を挿入した後に行を挿入できません

<?php 

    class UsersController { 

    public $_GB; 

    function __construct($_GB) 
{ 
    $this->_GB = $_GB; 
} 

public function VerifyUser($phone, $code, $countryName) 
{ 
    $phone = $this->_GB->_DB->escapeString($phone); 
    $countryName = $this->_GB->_DB->escapeString($countryName); 

    $app_name = $this->_GB->getSettings('app_name'); 
    $smsVerification = $this->_GB->getSettings('sms_verification'); 
    if ($smsVerification == 1) { 
     $smsVerification = true; 
    } else { 
     $smsVerification = false; 
    } 
    if (!$this->UserExists($phone, $countryName)) { 
     // Generating API key 
     $auth_token = $this->generateApiKey(); 


     $arrayData = array(
      'phone' => $phone, 
      'auth_token' => $auth_token, 
      'status' => 'Hey, follow and add me up on ' . $app_name, 
      'status_date' => time(), 
      'country' => $countryName, 
      'is_activated' => 0, 
      'has_backup' => 0, 
      'backup_hash' => null 

     ); 
     $result = $this->_GB->_DB->insert('users', $arrayData); 
     $newUserID = $this->_GB->_DB->last_Id(); 
     $this->insertDefaultStatus($newUserID); 
     // check if row inserted or not 
     if ($result) { 
      $IDResult = $this->_GB->_DB->select('users', '*', " `phone` = '{$phone}'"); 
      if ($this->_GB->_DB->numRows($IDResult) > 0) { 
       $fetch = $this->_GB->_DB->fetchAssoc($IDResult); 
       $res = $this->createCode($fetch['id'], $code); 
       if ($res) { 
        // successfully inserted into database 
        if ($smsVerification == true) { 
         $this->verificationCodeMessage($phone, $code); 
        } 
        $array = array(
         'success' => true, 
         'message' => 'SMS request has been initiated! Please wait, You will be receiving it shortly.', 
         'mobile' => $phone, 
         'smsVerification' => $smsVerification, 
         'code' => $code, 
         'hasBackup' => $fetch['has_backup'] == 1 ? true : false 
        ); 
        return $array; 
       } else { 
        // Failed to create user 
        $array = array(
         'success' => false, 
         'message' => 'Sorry! Something went wrong.', 
         'mobile' => null, 
         'smsVerification' => $smsVerification, 
         'code' => null, 
         'hasBackup' => false 
        ); 
        return $array; 
       } 
      } 

     } else { 
      // Failed to create user 
      $array = array(
       'success' => false, 
       'message' => 'SORRY! SOMETHING WENT WRONG2.', 
       'mobile' => null, 
       'smsVerification' => $smsVerification, 
       'code' => null, 
       'hasBackup' => false 
      ); 
      return $array; 

     } 
    } else if ($this->UserExists($phone, $countryName)) { 
     // User with same phone already existed in the database 

     // Generating API key 
     $auth_token = $this->generateApiKey(); 

     $fields = "`auth_token` = '" . $auth_token . "'"; 
     $fields .= ",`is_activated` = '" . 0 . "'"; 
     $result = $this->_GB->_DB->update('users', $fields, "`phone` = {$phone}"); 

     // check if row inserted or not 
     if ($result) { 
      $IDResult = $this->_GB->_DB->select('users', '*', " `phone` = '{$phone}'"); 
      if ($this->_GB->_DB->numRows($IDResult) > 0) { 
       $fetch = $this->_GB->_DB->fetchAssoc($IDResult); 
       $res = $this->createCode($fetch['id'], $code); 
       if ($res) { 
        // successfully inserted into database 
        // send sms 
        if ($smsVerification == true) { 
         $this->verificationCodeMessage($phone, $code); 
        } 
        $array = array(
         'success' => true, 
         'message' => 'SMS request has been initiated! Please wait, You will be receiving it shortly.', 
         'mobile' => $phone, 
         'smsVerification' => $smsVerification, 
         'code' => $code, 
         'hasBackup' => $fetch['has_backup'] == 1 ? true : false 
        ); 
        return $array; 

       } else { 
        // Failed to create user 
        $array = array(
         'success' => false, 
         'message' => 'Sorry! Something went wrong.', 
         'mobile' => null, 
         'smsVerification' => true, 
         'code' => null, 
         'hasBackup' => false 
        ); 
        return $array; 

       } 
      } 

     } else { 
      // Failed to create user 
      $array = array(
       'success' => false, 
       'message' => 'Sorry! Something went wrong.', 
       'mobile' => null, 
       'smsVerification' => $smsVerification, 
       'code' => null, 
       'hasBackup' => false 
      ); 
      return $array; 

     } 
    } else { 
     $array = array(
      'success' => false, 
      'message' => 'Sorry! mobile number is not valid or missing.', 
      'mobile' => null, 
      'smsVerification' => $smsVerification, 
      'code' => null, 
      'hasBackup' => false 
     ); 
     return $array; 
    } 

} 

} 

最初のユーザーを確認しようとするとうまくいきます。最初のユーザーAGAINを確認すると、既に存在しているユーザーの行が正常に更新されます。しかし、私は別のユーザーを確認しようとすると、何もデータベースに挿入されません。それは私に "SORRY!SOMETHING WENT WRONG2"を与えるだけです。

私のデータベースdb.sqlを参照してください。

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL, 
    `UserName` varchar(225) NOT NULL, 
    `UserStatus` varchar(100) NOT NULL, 
    `UserState` text NOT NULL, 
    `phone` varchar(255) NOT NULL, 
    `country` varchar(255) DEFAULT NULL, 
    `status` varchar(255) DEFAULT NULL, 
    `auth_token` varchar(32) NOT NULL, 
    `UserImage` varchar(100) DEFAULT NULL, 
    `UserCover` varchar(200) DEFAULT NULL, 
    `FullName` varchar(225) NOT NULL, 
    `Date` int(11) NOT NULL, 
    `active` int(11) DEFAULT '1', 
    `website` text, 
    `reg_id` text, 
    `isFollowing` text NOT NULL, 
    `status_date` int(11) NOT NULL, 
    `is_activated` int(1) NOT NULL DEFAULT '0', 
    `has_backup` int(1) NOT NULL DEFAULT '0', 
    `backup_hash` varchar(255) DEFAULT NULL 

    ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

    ALTER TABLE `users` 
    ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `Index_2` (`UserName`), ADD KEY `Index_3` (`active`); 

ALTER TABLE `users` 
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; 
+0

ユニークキーで同じ値を繰り返し使用できない場合は、挿入値を確認してください – praveena

答えて

0

UserNameには一意のキー制約がありますが、UserExists関数ではパラメータとして使用されません。同じ名前を追加しようとすると不可能になるため、UserExists関数のuserNameをチェックする必要があります。

+1

お返事ありがとうございます!私はそれを削除することを決意しました "追加一意キー' Index_2'( 'UserName')、追加キー' Index_3'( 'アクティブ') "。 –

0

おそらく同じユーザーの2番目のレコードを追加するのではなく、UPDATEレコードにしたいですか?

INSERT ... ON DUPLICATE KEY UPDATE ...を参照してください。

関連する問題