我有两个表"预订"和"时间表",当我在"预订"表中插入值时,我需要在另一个表中插入一些值,并更新第二个表中的字段
我的控制器
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);
}
我的模型
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();
}
检查座位
public function check_seat($data){
$this->db->where('seatLabel',$data['seatLabel']);
return $query = $this->db->get('booking')->num_rows();
}
从上面的代码中,当我插入值时,我收到一个 FALSE 结果,这意味着没有插入任何值,但我不知道问题出在哪里。
我会说问题是schedule
表。您指定的是 4 列的值,而不是所有列的值。
$schedule = array(
'ticketNumber'=>$bid ,
'paymentStatus'=>$paymentStatus,
'passengerName'=>$passengerName,
'agentId'=>$agentId,
);
$this->db->insert('schedule', $schedule);
如果您查看schedule
表中的其他列,例如seatLabel
或seatStatus
...等,它们没有阻止插入数据的默认值。因此,我希望解决方案要么为其他列设置默认值,要么确保在插入以下内容时填写所有数据:
$schedule = array(
'ticketNumber'=>$bid ,
'paymentStatus'=>$paymentStatus,
'passengerName'=>$passengerName,
'agentId'=>$agentId,
'seatLabel' => '',
'busNumber' => '',
...etc
);
$this->db->insert('schedule', $schedule);