为什么查询在数据库中插入两次,Codeigniter?

时间:2015-10-26 10:21:49

标签: php mysql codeigniter email

我正在尝试在函数$pindata中的数据库中插入pin/add数组,然后将用户重定向发送到Mail Controller的发送功能,只需向注册用户发送验证邮件{{1}成功插入数组,当它重定向到mail / send函数时,send函数将$ pindata数组插入到数据库中,这使得两条记录具有相同的数据插入数据库(重复记录),我试图调试,我最终得到 $pindata此行再次向数据库插入$this->email->send();。任何帮助将不胜感激,并提前感谢。 Plz点击链接$pindata

在用户控制器中

http://localhost/addypin/user/add

}

邮件控制器

class User extends MY_Controller {
function __construct() {
    parent::__construct();

}

function index() {
    $this->session->sess_destroy();
}

function add() {
    // get email from HTML form.
    $id = 0;

    $data = array(
        'email'     => 'a.ali2010@yahoo.com',
        'url'       => $this->createUserUrl() . '.addypin.com',
        'pinscount' => 0,
        'type'      => 'personal'
    );

    $this->user_m->login($data);

    $user = $this->user_m->get_by(array('email'=> $data['email']), TRUE);

    if($user) {
        foreach ($user as $foundeduser) {
            if($foundeduser->pinscount == 5 && $foundeduser->type == 'personal') {
                die('Sorry, You have reached the maximum pins number as Personal Account.');
            } else if($foundeduser->pinscount == 20 && $foundeduser->type == 'corporate') {
                die('Sorry, You have reached the maximum pins number as Corporate Account.');
            } else {
                $data['pinscount'] = $foundeduser->pinscount;
                $data['id'] = $foundeduser->id;
                $this->user_m->login($data);    
                redirect('pin/add');
            }
        }           
    } else {
        $id = $this->user_m->save($data);
        $data['id'] = $id;
        $this->user_m->login($data);
        redirect('pin/add');
    }
}

function getPinsCount() {
    return $this->user_m->getPinsCount();

}

function createUserUrl() {
    return $this->user_m->createUserUrl();
}

function checksession() {
    if($this->user_m->checksession()) {
        echo 'expired';
    } else {
        echo 'NotExpired';
    }
}

}

引脚控制器

class Mail extends Frontend_Controller {
function __construct() {
    parent::__construct();
    $this->load->library('email');
    $this->load->model('pin_m');
    $this->load->model('user_m');
}

function index() {
    // this function for test only.
    echo "hi";
}

function send() {
    $confCode = $this->uri->segment(3);

    $this->email->set_newline("\r\n");
    $this->email->from('addyourpin@gmail.com');
    $this->email->to('a.ali2010.fci@gmail.com');
    $this->email->subject('Email test');
    $this->email->message('Hi User thanks for registration, click the link below to activate your account '. anchor("http://localhost/addypin/mail/verify/".$confCode, 'Activate Account'));
    $this->email->set_mailtype("html");

    if($this->email->send()) {
        echo "Email was sent successufly";          
    } else {
        show_error($this->email->print_debugger());
    }

    redirect('mail/demo');
}

function verify() {
    $confCode = $this->uri->segment(3);
    $data = array(
        'confirmationcode' => $confCode 
    );

    $pin = $this->pin_m->get_by($data, TRUE);
    if($pin) {
        foreach ($pin as $row) {
            if($confCode === $row->confirmationcode) {
                $registerTime = $row->regtime;
                $oneWeekLater = $registerTime + (7 * 24 * 60 * 60);
                if(time() > $oneWeekLater) {
                    // Confirmation expires.
                    die('Registration date more than one week later Failed');
                } else {
                    // Confirmation done.
                    $active = array('active' => 1);
                    $this->pin_m->save($active, $row->id);
                    $this->user_m->save(array('pinscount' => $this->user_m->getPinsCount() + 1), $this->session->userdata('id'));
                    redirect('pin/');
                }
            } 
            // confirmation code not match the one in the database.
            else die('False confirmation code');
        }
    }

}

function demo() {
    echo 'One click to go, verify your account.';
}

}

在模型中

class Pin extends MY_Controller {
function __construct() {
    parent::__construct();
    $this->load->model('pin_m');
}

function index() {
    echo "Hi again";
}

function add() {
    $confCode = random_str();
    $pindata = array(
        'name'                  => 'My home', 
        'location'              => 'Cairo',
        'code'                  => 'SDSDSD',
        'postalcode'            => '123321',
        'active'                => 0,
        'user_id'               => $this->session->userdata('id'),
        'confirmationcode'      => $confCode,
        'latLang'               => '',
        'regtime'               => time()
    );

    $this->pin_m->save($pindata);
    redirect('mail/send/'. $confCode);
}

function edit() {
    $data = $this->pin_m->get(9);
    echo print_r($data);

}

function share() {

}

function delete() {

}

}

2 个答案:

答案 0 :(得分:0)

包括此

$this->email->set_mailtype("html");

默认情况下, 纯文本

在模型中

public function save($data) {

    $id = $data['user_id'];
    $data1 = array(
           'name'               => $data['name'],
           'location'           => $data['location'],
           'code'               => $data['code'],
           'postalcode'         => $data['postalcode'],
           'active'             => $data['active'],
           'confirmationcode'   => $data['confirmationcode'],
           'latLang'            => $data['latLang'],
           'regtime'            => $data['regtime'],
        );


    $this->db->where('id', $id);
    $this->db->update('table_name', $data1); 
}

您正在将一个值传递给模型$this->pin_m->save($pindata);。但在模型中你会遇到两个public function save($data, $id = NULL) {

答案 1 :(得分:0)

试试这个..

public function save($data) {

$id = $data['user_id'];
$data_array = array(
       'name'               => $data['name'],
       'location'           => $data['location'],
       'code'               => $data['code'],
       'postalcode'         => $data['postalcode'],
       'active'             => $data['active'],
       'confirmationcode'   => $data['confirmationcode'],
       'latLang'            => $data['latLang'],
       'regtime'            => $data['regtime'],
    );


$this->db->where('id', $id);
$this->db->update('table_name', $data_array); 

}