0

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

}

10
  • Paste code of demo function also !! Commented Oct 26, 2015 at 10:28
  • also add model function. Commented Oct 26, 2015 at 10:29
  • @NiranjanNRaju i added the whole model check now Commented Oct 26, 2015 at 11:16
  • @Saty i added the demo function Commented Oct 26, 2015 at 11:17
  • add this.. just a try $this->pin_m->save($pindata,$pindata['user_id']); Commented Oct 26, 2015 at 11:32

2 Answers 2

0

Try this..

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); 

}

Sign up to request clarification or add additional context in comments.

Comments

0

Include this

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

By default it is Plain Text

In Model

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

You are passing one value to model $this->pin_m->save($pindata);. But in model you catch two public function save($data, $id = NULL) {

13 Comments

i added the model function save();
it is only one function for update and insert based on the id if passed the id it will make update and if not it will make insert where two save function u meant abdulla ??
i tried it for many situation and it works fine and also tried to make to function for save and insert and also i get the same problem here
where is the demo() function?
What is the URL in your browser after doing all these operation?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.