0

Im encourting a problem with my "add" function on the "subscriptions" model. 1% of the registerations is duplicated(2 or even up to 5 times) for some reason.

this is the code im using:

public function add($service, $phone, $sushi_subscription_id, $answer_id, $affiliate_id, $ip, $query, $invite_type, $invite_msg_id)
{
    $this->db->set('service_id', $service->id);
    $this->db->set('sushi_service_id', $service->sushi_service_id);
    $this->db->set('phone', $phone);

    if ($sushi_subscription_id)
    {
        $this->db->set('sushi_subscription_id', $sushi_subscription_id);
    }

    $this->db->set('answer_id', $answer_id);
    if ($affiliate_id)
    {
        $this->db->set('affiliate_id', $affiliate_id);
    }

    $this->db->set('added', 'NOW()', FALSE);

    $this->db->set('active', 1);
    $this->db->set('ip', $ip);

    $this->db->set('query', $query);

    if ($invite_type)
    {
        $this->db->set('invite_type', $invite_type);
    }

    if ($invite_msg_id)
    {
        $this->db->set('invite_msg_id', $invite_msg_id);
    }

    return ($this->db->insert($this->_table_name)) ? $this->db->insert_id() : FALSE;
}

any idea how I could avoid this from happaning? the row is exactly the same. service_id, phone, active, even the added date!

1
  • If it is not duplication ALL the time, then your issue is most likely with the code that calls this function. Please post the controller code that calls the add method. Commented Nov 19, 2013 at 22:24

2 Answers 2

1

Although it is not totally irrelevant but it is very useful to build up your own generic functions or modules that serves your requirements. Here is check_duplicate function working as generic function checking duplicate data entry in DB. Here is the function

function _check_duplicate($table_name,$param)
{
    $row_count=$this->db->get_where($table_name,$param)->num_rows();
    if(count($row_count>0))
    {
        return true;
    }
    else
    {
        return false;
    }
}

here is how you call the function;

if($this->_check_duplicate('table_name',array('param1'=>$param1,'param2'=>$param2)))
{
    // redirect to some page
}
else
{
    // do something, insert or update
}

for parameters, you can pass as many parameters as required. this function can be generic in a controller or can be part of generic module. It depends how you use it.

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

Comments

1

I think this code will help you.....

//model
function add($data){
        $this->db->insert($this->table, $data);
        $this->session->set_flashdata("message", "Record successfully created.");
}


//in your controller
 function create(){
        $data = array( 'service_id'             => $this->input->post('service_id'),
                        'sushi_service_id'      => $this->input->post('sushi_service_id'),
                        'phone'                 => $this->input->post('phone'),
                        'sushi_subscription_id' => $this->input->post('sushi_subscription_id'),
                        'answer_id'             => $this->input->post('answer_id'),
                        'affiliate_id'          => $this->input->post('affiliate_id'),
                        ''                      => $this->input->post(), // and so on and so on just like that copy and do that
                        //add more here
                        );
        $this->"model name"->add($data);
    }


//view
<form action="<?=site_url('controller_name/create');?>" method = "POST">//the word create is the function name and dont forget to change the controller name
    <input type="text" name="service_id"/>
    <input type="text" name="sushi_service_id"/>
    <input type="text" name="phone"/>
    //and so on and so on....just copy and do like that
    <input type="submit"/>
</form>

Comments

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.