重复键插入上的Codeigniter

时间:2017-08-06 08:30:05

标签: php codeigniter dropdown insert-into

我想创建一个条件,用户可以选择是否要使用输入,这意味着数据库会创建auto_incremented id,或者他想使用不使用新id但只使用它的旧数据。我使用下拉数据库填充我的旧输入。

我的下拉列表

//get contractor list
    $Contractor_List = $this->foo_pro->get_list_contractors();
    $opt = array('' => '');
    foreach ($Contractor_List as $Contractor_No) {
       $opt[$Contractor_No] = $Contractor_No;
    }
    $data['con_list'] = form_dropdown('',$opt,'','ProjectID = "Contractor_No" name="contractorNo" id="" class="w3-select w3-border w3-hover-light-grey"');

我的控制器

public function save_createdProject()
{   

    $data_project = array();
    $data_contractor = array();       

    //project data
    if ($this->input->post('year') === '') {
        $data_project['P_Year'] = 15;
    }else{
        $data_project['P_Year'] = $this->input->post('year');
    }
    if ($this->input->post('code') === '') {
        $data_project['Code'] = 'KO';
    }else{
        $data_project['Code'] = $this->input->post('code');
    }
    $data_project['ProjectID'] = $this->input->post('project');
    $data_project['Contract_Amount'] = $this->input->post('camount');

    //contractor data
    if ($this->input->post('cname') === '' && $this->input->post('caddress') === '') {
        $data_project['Contractor_No'] = $this->input->post('contractorNo');
    }else{
        $data_contractor['Contractor_Name'] = $this->input->post('cname');
        $data_contractor['Contractor_Address'] = $this->input->post('caddress');
    }

    $this->foo_pro->add_project($data_project, $data_contractor);

    redirect('Main/project','refresh');
    //var_dump($data);exit;
}

我的模特

public function add_project($data_project, $data_contractor)
{   
    //contractor
    $this->db->insert('contractor', $data_contractor);
    $Contractor_No = $this->db->insert_id();
    //project
    $data_project['Contractor_No'] = $Contractor_No;
    $this->db->insert('project', $data_project);
    $ProjectID = $this->db->insert_id();

}

这是我的问题,除了插入旧数据,这是contractor_no 1它创建了另一个数据,将作为contractor_no 4插入..但我还需要为新的contractor_name插入新数据..

contractor

0 个答案:

没有答案