Home > other >  Insert data array on php
Insert data array on php

Time:09-28

This is code I wrote:

foreach ($quotefile as $quote_files) {
    $clone_quote = str_replace('PP', 'PR', $quote_files);
    $file = 'uploads/quotation/' . $quote_files;
    $newfile = 'uploads/pr/' . $clone_quote;
    copy($file, $newfile);
    $files[] = array(
        'PONO' =>  $pr_no,
        'PP_ID' =>  $pp_id,
        'FILENAME' => $clone_quote,
    );
    $this->M_pr->send_pr($files);
}

And this send_pr() function:

function send_pr($files)
{
    $data = $this->db->insert_batch('PR_FILE',$files);
    return $data;
}

This is the output that goes to the database:

enter image description here

why are some data rows repeating like that? I want the data to insert into the database, not duplicates.

CodePudding user response:

Since you are calling insert function inside loop, so it's getting repeated, move it outside

foreach ($quotefile as $quote_files) {
  // your  initial code
}
$this->M_pr->send_pr($files);// put this line outside of loop
  • Related