I am trying to insert $pindata array in database in function pin/add, then send user redirect to send function of Mail Controller, to just send verification email to the registered user, the $pindata array get inserted successfully and when it redirected to mail/send function the send function insert $pindata array to the database also which makes two records with the same data inserted to databse (duplicate record), i tried to debug and i end up with
$this->email->send(); this line is inserting $pindata again to database. any help will be appreciated, and thanks in advance. Plz follow the link http://localhost/addypin/user/add .
In user Controller
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' => '[email protected]',
'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';
}
}
}
mail controller
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('[email protected]');
$this->email->to('[email protected]');
$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.';
}
}
Pin controller
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() {
}
}
In Model
class MY_Model extends CI_Model {
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by;
protected $_timestamps = FALSE;
public $rules = array();
function __construct() {
parent::__construct();
}
public function get($id = NULL, $single = FALSE) {
if($id === NULL) {
$method = 'result';
} else if($single === TRUE) {
$method = 'row';
} else {
// filter id for security issues
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($data, $single = FALSE) {
$this->db->where($data);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL) {
// insert
if($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
// update
else {
// filter id for security issues
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$this->db->set($data);
$this->db->update($this->_table_name);
}
return $id;
}
public function delete($id) {
$filter = $this->_primary_filter;
$id = $filter($id);
if(!$id) return FALSE;
$this->db->where($this->_primary_key, $id);
if($this->db->delete($this->_table_name))
return TRUE;
}
private function filter($id) {
$filter = $this->_primary_filter;
$id = $filter($id);
return $id;
}
}
demofunction also !!