Welcome Guest, Not a member yet? Register   Sign In
Foreach Insert Problem
#6

You say you want to enter a new ID for each invoice (Rechnung) in the form.
But in your model, you let the database determine the new ID for the invoice:

PHP Code:
$this->db->insert('db_rechnung'$data);
$rechnung_id $this->db->insert_id(); 

If the ID-field is an auto-increment field, you can't use a POST value from a form to store it's value. So there is no need to ask for in the form.
I wonder whether you need a form in the first place. Why don't you simply read all invoices one by one (in a foreach loop), remember the current ID (in an variable); then save the record into the other table you have, and then do update in the db_artikel table:
PHP Code:
//This code goes in your controller:
$rechnungs $this->Setup_model->rechnungs();   //result should be an array of arrays
foreach ($rechnungs as $rechnung)
{
 
  $rechnung_new $rechnung;
 
  unset($rechnung_new['id']);   //remove the auto-increment ID from the record that is going to be inserted
 
  $id_old $rechnung['id'];
 
  $this->db->insert('db_rechnung_new',$rechnung_new);
 
  $id_new $this->db->insert_id();
 
  $this->db->where('rechnung_id',$id_old);
 
  $this->db->set('rechnung_id',$id_new);
 
  $this->db->update('db_artikel');  //the old rechnung_id for the artikel is replaced by the new rechnung_id
}

echo 
"Done!"
Reply


Messages In This Thread
Foreach Insert Problem - by Germanikus - 12-26-2017, 03:02 AM
RE: Foreach Insert Problem - by Wouter60 - 12-26-2017, 06:09 AM
RE: Foreach Insert Problem - by Germanikus - 12-26-2017, 06:21 AM
RE: Foreach Insert Problem - by XtreemDeveloper - 12-26-2017, 07:09 AM
RE: Foreach Insert Problem - by Germanikus - 12-26-2017, 08:56 AM
RE: Foreach Insert Problem - by Wouter60 - 12-26-2017, 12:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB