Php 重复行编码点火器

Php 重复行编码点火器,php,mysql,codeigniter,Php,Mysql,Codeigniter,我在“订阅”模型上的“添加”功能遇到问题。 由于某种原因,1%的注册表被复制(2次甚至最多5次) 这是我使用的代码: 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);

我在“订阅”模型上的“添加”功能遇到问题。 由于某种原因,1%的注册表被复制(2次甚至最多5次)

这是我使用的代码:

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;
}
你知道我怎样才能避免这种事发生吗?这一行完全一样。
服务id、电话、活动,甚至是添加的日期

我认为此代码将帮助您

//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>
//模型
函数添加($data){
$this->db->insert($this->table,$data);
$this->session->set_flashdata(“消息”,“记录已成功创建”);
}
//在控制器中
函数create(){
$data=array('service\u id'=>$this->input->post('service\u 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\u id'=>this->input->post('answer\u id'),
'affiliate_id'=>this->input->post('affiliate_id'),
''=>$this->input->post(),//等等,就像那个拷贝一样
//在这里添加更多
);
$this->“model name”->添加($data);
}
//看法

虽然这并非完全无关,但构建满足您需求的通用功能或模块非常有用。这里有一个check_duplicate函数,用作检查数据库中重复数据条目的通用函数。下面是函数

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;
    }
}
下面是如何调用函数

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

对于参数,可以根据需要传递任意多个参数。此函数可以是控制器中的通用函数,也可以是通用模块的一部分。这取决于您如何使用它。

如果它不是一直重复的,那么您的问题很可能与调用此函数的代码有关。请发布调用
add
方法的控制器代码。