• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
two database insert operations in one function using $this->db->insert()

#1
[eluser]core-coder[/eluser]
Hi all

How can I perform two insert operations to two tables in a function ?
I tried following code

Code:
$this->subproduct  = $name;
        $this->status = 1;
        $this->db->insert('subproduct', $this);
        $insert_id = $this->db->insert_id();

        $this->subp_id = 2;
        $this->prd_id = $insert_id;
        $this->db->insert('product_subproduct', $this);

I am getting following error

Quote:A Database Error Occurred
Error Number: 1054

Unknown column 'subproduct' in 'field list'

INSERT INTO `product_subproduct` (`subproduct`, `status`, `subp_id`, `prd_id`) VALUES ('xxx', 1, 2, 19)


How can I reset values in $this after first insertion ?


Thanks

#2
[eluser]Cro_Crx[/eluser]
You shouldn't really be using the current object to insert into the database, it would probably be better to use another variable. That way you can use two different ones for the inserts, for example

Code:
$data['subproduct']  = $name;
$data['status'] = 1;
$this->db->insert('subproduct', $data);
$insert_id = $this->db->insert_id();

$other_data['subp_id'] = 2;
$other_data['prd_id'] = $insert_id;
$this->db->insert('product_subproduct', $other_data);

You could use anything else instead of $data and $other_data

#3
[eluser]saidai jagan[/eluser]
Yes this is Clean Smile

#4
[eluser]core-coder[/eluser]
Thanks guys


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.