Search code examples
phpmysqldatabasecodeignitercodeigniter-3

How to copy a row from database table and insert it to the same table using codeigniter


i have a codeigniter website with a table containing some values, i want to duplicate the row from the table, i did the following code:

    $id = $this->uri->segment(3);
    $details = $this->db->where("id", $id)->get('tbl_invoices');
    

    foreach ($details->result() as $row) {
          $this->db->insert('tbl_invoices',$row);
    }

however this gives me error, can anyone please tell me how to accomplis this, thanks in advance

The error is

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0) VALUES (Array)' at line 1 INSERT INTO tbl_invoices (0) VALUES (Array)


Solution

  • Use unset($row->id); in the foreach loop.

     foreach ($details->result() as $row) {
               unset($row->id);
              $this->db->insert('tbl_invoices',$row);
        }