Fahmi Salmin
Fahmi Salmin

Reputation: 25

CODEIGNITER how to insert user_ID to mySQL when that user_ID is logged in

this is the error page

Cannot add or update a child row: a foreign key constraint fails (latihan_ci.adventure, CONSTRAINT adventure_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE)

INSERT INTO adventure (name, category, place, state) VALUES ('semeru', 'gunung', 'asa', 'asd')

and this is my controller

public function addTrip(){
    $this->load->model('userModel');
    $newTrip = ['name' => $this->input->post('name'),
                'category' => $this->input->post('category'),
                'place' => $this->input->post('place'),
                'state' => $this->input->post('state'),

                ];
            $data['users'] = $this->userModel->getUserId()->result(); 

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

    redirect('userController/profile');
}

and this is my Model

public function getUserId()
    {
        return $this->db->get('users',['user_id']);
    }

so how to add the user_ID to the mySQL when the value is user_ID that is already login..Thanks alot

Upvotes: 0

Views: 136

Answers (1)

Rushi
Rushi

Reputation: 71

when you are login at that time user_id store in session after use

controller

$newTrip = ['name' => $this->input->post('name'),
            'category' => $this->input->post('category'),
            'place' => $this->input->post('place'),
            'state' => $this->input->post('state'),
            'user_id'=>$this->session->userdata('userdata'),
];

Upvotes: 1

Related Questions