douruanfan3030 2017-03-06 04:43
浏览 72
已采纳

在mysql中插入第一行后无法插入行

I am working on a user verification form that inserts user's phone number and country into database before verifying. But, after inserting the first row, it does not insert a second row. In my 'user' mysql table, I have 'id' as my primary key with AUTO_INCREMENT.

<?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;
    }

}

}

When I try to verify the first user, it works fine. When verify the first user AGAIN, it successfully updates the user row SINCE IT IS ALREADY EXISTING. But when i try to verify another user, nothing is inserted into database; it only gives me "SORRY! SOMETHING WENT WRONG2"

see my database 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;
  • 写回答

2条回答 默认 最新

  • douyan1972 2017-03-06 06:20
    关注

    You have a unique key constraint on the UserName but your UserExists function doesn't take it in parameter. Maybe you should check the userName in the UserExists function because if you try to add the same name it will be impossible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料