Ganesh Aher
Ganesh Aher

Reputation: 1128

how to insert data in two separate tables using single query in codeigniter?

I want to send form data in two different tables. Is it possible in codeigniter ? I do google for this and I found that it is not possible in MySQL. I code for that in my model :

public function create($icon = '')
{
    $data = array(
        'title' => $this->input->post('title', true),
        'icon' => $icon,
        'description' => $this->input->post('description', true),
        'price' => $this->input->post('price', true),
        'id_admin' => $this->input->post('id_admin', true),
        'link' => $this->input->post('link', true),
        'sort_order' => $this->input->post('sort_order', true),
    );

    return $this->db->insert('products', $data);

    $data_view = array(
        'title' => $this->input->post('title', true),
        'id_admin' => $this->input->post('id_admin', true),
    );

    return $this->db->insert('product_view', $data_view);
}

and in my controller I'm echoing that using

echo json_encode($json);

It insert data into my first table i.e. in

product

table but not in

product_view

table.

Is it possible to do so in codeigniter. I made any mistake. Please help me. Thanks in advance.

Upvotes: 0

Views: 4093

Answers (3)

Nishant Nair
Nishant Nair

Reputation: 2007

Try using below

    public function create($icon = '')
    {
        $data = array(
            'title' => $this->input->post('title', true),
            'icon' => $icon,
            'description' => $this->input->post('description', true),
            'price' => $this->input->post('price', true),
            'id_admin' => $this->input->post('id_admin', true),
            'link' => $this->input->post('link', true),
            'sort_order' => $this->input->post('sort_order', true),
        );

        $id = $this->db->insert('products', $data);

        if($id){
        $data_view = array(
            'title' => $this->input->post('title', true),
            'id_admin' => $this->input->post('id_admin', true),
        );

        $product_id = $this->db->insert('product_view', $data_view);
            if($product_id){
            return "successfully inserted";
            }
        }
        return "Unable to insert product";

    }

Upvotes: 2

user3578842
user3578842

Reputation: 11

in your code you return the pointer with insert (return $this->db->insert('products', $data);) just remove return from first insert problem will solve just write ($this->db->insert('products', $data);)

Upvotes: 1

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Not need to return anything..if you want to just insert.Dont forget to load database.

$this->load->database();

THEN

public function create($icon = '')
{
    $data = array(
        'title' => $this->input->post('title', true),
        'icon' => $icon,
        'description' => $this->input->post('description', true),
        'price' => $this->input->post('price', true),
        'id_admin' => $this->input->post('id_admin', true),
        'link' => $this->input->post('link', true),
        'sort_order' => $this->input->post('sort_order', true),
    );

    $this->db->insert('products', $data);

    $data_view = array(
        'title' => $this->input->post('title', true),
        'id_admin' => $this->input->post('id_admin', true),
    );

     $this->db->insert('product_view', $data_view);
}

Upvotes: 2

Related Questions