简体   繁体   中英

How to escape duplicated value from excel and try to insert into database in Codeigniter?

Dear Friend I creating Import feature in Codeigniter using below function but I don't know how to escape when duplicated value happen on my data when my loop selected from excel The Primary key is only used for $in_val['in_no'] and other value is free to insert. How to escape duplicated value and try to insert other row from excel into database?

Here is my Function

public function imported($file_paths) {
        $in_val = array();
        $i = 0;
        $cell_array = $this->csvimport->get_array($file_paths);
        foreach ($cell_array as $row) {
            if ($i === 7000)break; {
                $in_val['user_id'] = 70;
                $in_val['in_no'] = (int) preg_replace("/(\D+\d{4})0*([1-9]+\d*)/i", "$2\n", trim($row['IN_number']));
                $in_val['fi_no'] = trim($row['Fir_obtic']);
                $in_val['paid_by'] = trim($row['Paid_by']);
                $in_val['in_per'] = str_replace('/', '-', trim($row['In_date']));
                $in_val['sale_by'] = trim($row['Sale_by']);
                $in_val['billed'] = trim($row['Billed']);
                $in_val['in_date'] = str_replace('/', '-', trim($row['In_date']));
                $in_val['top_up_num'] = trim($row['T_P_number']);
                $in_val['ffrom'] = trim(str_replace('/', '-', $row['From']));
                $in_val['fto'] = str_replace('/', '-', trim($row['to']));
                $in_val['pay_du_date'] = str_replace('/', '-', trim($row['Pay_due_date']));
                $in_val['bill_amount'] = trim(str_replace('/', '-', $row['Bill_Amount']));
                $in_val['bill_to'] = trim($row['Cust_name']); //customer name
                $in_val['receiv_date'] = trim($row['R_C_date']);
                $in_val['adds'] = trim($row['Address']);
                $in_val['bill_item'] = str_replace('/', '-', trim($row['Bill_Item']));
                $in_val['descr'] = trim($row['Description']);
                $in_val['in_type'] = strtolower(preg_replace("/(\D+\d{0})0*([0-9]+\d*)/i", "$1\n", trim($row['IN_number'])));
                $in_val['quant'] = trim($row['In_Quanity']);
                $in_val['unit_p'] = trim($row['In_Unit_P']);
                $in_val['amount'] = trim($row['Bill_Amount']);
                $in_val['total'] = trim($row['Total']);
                $in_val['status'] = 1;
                $in_val['amc'] = trim($row['Amount_Description']);
                $pri_val['user_id'] = 70;
                $pri_val['type'] = strtolower(preg_replace("/(\D+\d{4})0*([1-9]+\d*)/i", "$1\n", trim($row['IN_number'])));
                $pri_val['parent'] = 1;
                $pri_val['status'] = 1;
                $pri_val['in_no'] = (int) $in_val['in_no'];
                $pri_val['total'] = (int) trim($row['Total']);
                $pri_val['in_date'] = $in_val['in_date'];
                $this->db->insert('dbfinan_invoice', $in_val);
                $this->db->insert('prifix', $pri_val);
                $i++;
            }
        }
    }

Please help

try add condition and function to check in table like this:

if($this->[yourmodal]->isExist($in_val['in_no'])==false)
{
  $this->db->insert('dbfinan_invoice', $in_val);
  $this->db->insert('prifix', $pri_val);
}

So $in_val['in_no'] in table can unique

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM