简体   繁体   中英

codeigniter insert into multiple tables

I have a table named testimonial . The table structure is as follows.

在此输入图像描述

But the table dont seem normalized. I would like somthing like.

trip_testimonial

-----------------------------------
id
trip_id
status
-----------------------------------

And another table as

testimonial

   ----------------------------------
    id
    name
    email
    website
    message
    -----------------------------------

But how do I enter the data into them in codeigniter. I have used something like

function add($data){

        $this->db->insert('testimonials', $data);
        if($this->db->affected_rows()>0){
            return TRUE;
        }else{
            return FALSE;
        }
    }

What would be the best approach??

[TRIP]
trip_id
trip_name
trip_desc
....

[TESTIMONIAL]
testimonial_id
trip_id
testim_name
testim_email
......

function add($data){

  $dataTrip = array(
    //fill the array the appropriate data
  );

  //return the last inserted trip_id
  $lastTripId = $this->myModel->myFuncToAddTrip($dataTrip);    

  $dataTestimonial = array(
   'trip_id' => $lastTripId,
   'testim_name' => $data['testim_name'],
   .......
  );

  if($this->myModel->myFuncAddTestimonial($dataTestimonial)){
   //success
  }else{
   //error
  }

}

Use transactions to roll back if at least 1 query fails.

EDIT 1 trip to many testimonials

Your structure is normalized for one to one relationship just remove the trip_id column

testimonial
-----------------------------------
id
name
email
website
message
date
status

Your add function is fine

function add($data){

    $this->db->insert('testimonials', $data);
    if($this->db->affected_rows()>0){
        return TRUE;
    }else{
        return FALSE;
    }
}

And the data to be inserted

$data['name']       =   'blah';
$data['email']      =   'abc@test.com';
$data['website']    =   'website';
$data['message']    =   'message';
$data['date']       =   date('Y-m-d H:i:s');

And set the default value of status to 0 at table creation or with ALTER.
But when you need to insert you can add one more column with different value.

$data['status']     =   1;

And now you can call function add
When selecting you can select with status

SELECT * FROM testimonial WHERE status = 0

OR

SELECT * FROM testimonial WHERE status = 1

UPDATES:

If this is the case than you can do it like this. Add a column to the testimonials table

testimonial
-----------------------------------
id
name
email
website
message
date
status
trip_id

Now select trip id

    SELECT id FROM trip_testimonial WHERE status = 0

And the returned id should be put with testimonial data

$data['name']       =   'blah';
$data['email']      =   'abc@test.com';
$data['website']    =   'website';
$data['message']    =   'message';
$data['date']       =   date('Y-m-d H:i:s');
$data['trip_id']    =   $row->id;   

And now call your add function

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