How to insert and update value in two different tables in codeigniter


Ask A Question





Login To post a question

I have two tables ‘booking’ and ‘schedule’ and when i insert values in ‘booking’ table i need to insert some value in another table and also update a field in second table

My controller

 public function booking(){
        $data = $_POST; 
        $check_seat = $this->Web_model->check_seat($data);
        if($check_seat>0){
            $result =  NULL;
        } else {
            $booking =$this->Web_model->booking_details($data);
            if($booking){
                $result =  $booking;
            } else {
                $result =  FALSE;
            }           
        }               
        print json_encode($result);
    }

My model

 public function booking_details( $data){
                   $booking = array();
                   $bid ='TKT'.strtotime(date('m/d/Y H:i:s'));
                   $occupied = 'Occupied';
                   $busNumber = $data['busNumber'];
                   $bookingDate = $data['bookingDate'];
                   $seatLabel = $data['seatLabel'];
                   $passengerName = $data['passengerName'];
                   $paymentStatus = $data['paymentStatus'];
                   $agentId = $data['agentId'];
                   $booking = array(
                'ticketNumber'=>$bid ,
                'busNumber'=>$busNumber,
                'seatLabel'=>$seatLabel ,
                'paymentStatus'=>$paymentStatus,
                'passengerName'=>$passengerName,
                'passengerPhone'=>$data['passangerPhone'],  
                'farePaid'=>$data['farePaid'],              
                'boarding'=>$data['anakopandia'],
                'dropping'=>$data['anakoshukia'],
                'bookingDate'=>$bookingDate,    
                'agentGroup'=>$data['agentGroup'],
                'agentId'=>$agentId,
                'agentName'=>$data['agentName'],
            );

            $this->db->insert('booking', $booking);
            $last_insert_id = $this->db->insert_id();

            $schedule = array(
                'ticketNumber'=>$bid ,                          
                'paymentStatus'=>$paymentStatus,
                'passengerName'=>$passengerName,                        
                'agentId'=>$agentId,

            );      

            $this->db->insert('schedule', $schedule);
            $this->db->where('bookingDate',$bookingDate);
            $this->db->where('busNumber',$busNumber);
            $this->db->where('seatLabel',$seatLabel);
            $seatStatus['seatStatus'] = $occupied;
                $this->db->update('schedule',$seatStatus);          

            $query = $this->db->get_where('booking',array('id' => $last_insert_id));
                return $query->row();
                }

check seat

public function check_seat($data){  
        $this->db->where('seatLabel',$data['seatLabel']);
        return $query = $this->db->get('booking')->num_rows();  
                   }

From the above codes when i insert values i receive a FALSE result that means no value inserted but i don’t know where is the problem.

schedule table

booking table

For more info please click herehttps://stackoverflow.com/q/51824326



Have a question or need a custom quote?
info@smartinfocare.com