[英]Codeigniter - inserting into multiple tables
I am having problems with a booking form in Codeigniter; 我在Codeigniter的预订单时遇到问题; when a visitor makes a booking, the details are supposed to be inserted into two tables in a database ('reservation', and 'period', there is an FK from 'reservation' to 'period').
当访客进行预订时,应将详细信息插入数据库的两个表中(“预订”和“期间”,从“预订”到“期间”有一个FK)。
When I try to make the booking, nothing appears in the database, and I can't tell if it a problem with the form, or between the controller and model (there are no errors thrown - just nothing in the db). 当我尝试进行预订时,数据库中没有任何内容,而且我无法说出表单或控制器与模型之间是否存在问题(没有引发错误-数据库中没有任何错误)。 I tried to follow the logic from this example .
我试图遵循此示例中的逻辑。
here is my form (gite-booking-form.php): 这是我的表格(gite-booking-form.php):
<html>
<head>
<title>Gite booking form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('GiteCtl/giteBookingForm'); ?>
<h5>Select a gite:</h5>
<?php
$gitenames = array('giteA', 'giteB', 'giteC', 'giteD');
$gitename = isset($_POST['gitename']) && in_array($_POST['gitename'], $gitenames) ? $_POST['gitename'] : 'giteA';
echo '<select name="gitename">';
foreach ($gitenames as $option) {
echo '<option value="' . $option . '"' . (strcmp($option, $gitename) == 0 ? ' selected="selected"' : '') . '>' . $option . '</option>';
}
echo '</select>';
?>
<h5>Start date</h5>
<input type="date" value="<?php echo set_value('start_date'); ?>"/>
<h5>End date</h5>
<input type="date" value="<?php echo set_value('end_date'); ?>"/>
<div><input type="submit" value="Submit"/></div>
</form>
</body>
</html>
my model (Gite.php): 我的模型(Gite.php):
<?php
class Gite extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function reservationInsert($table, $data)
{
$query = $this->db->insert($table, $data);
return $this->db->insert_id();
}
}
and the controller: 和控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class GiteCtl extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Gite');
$this->load->library('calendar');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('Visitors');
}
public function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
if (!isset($isLoggedIn) || $isLoggedIn != TRUE) {
echo 'In order to book a gite, you must first login';
echo 'button: visitor-login.php'; // I will add this later
} else {
$this->giteBookingForm();
}
}
public function giteBookingForm()
{
$this->form_validation->set_rules('gite_selection', 'gite_select', 'required');
$this->form_validation->set_rules('start_date', 'startDate', 'required', array('required' => 'You must choose a %s.'));
$this->form_validation->set_rules('end_date', 'endDate', 'required', array('required' => 'You must provide an %s.')
);
if ($this->form_validation->run() == FALSE) {
$this->load->view('gite-booking-form');
} else {
$start_date = $this->input->post('start_date');
$end_date = $this->input->post('end_date');
$isHauteSaison = 1;
$isWeekend = 1;
$period_data = array(
'start_date' => $start_date,
'end_date' => $end_date,
'isHauteSaison' => $isHauteSaison,
'isWeekend' => $isWeekend
);
$gite_selection = $this->input->post('gite_selection');
$visitor_email = $this->session['visitorName'];
$price = 100.00;
$period_id = $this->Gite->reservationInsert('period', $period_data);
$reservation_data = array(
'gite_selection' => $gite_selection,
'visitor_email' => $visitor_email,
'price' => $price,
'period' => $period_id
);
$insert = $this->Gite->reservationInsert('reservation', $reservation_data);
}
}
}
Thanks, E. 谢谢,E。
In the end it was the form; 最后是表格。 the php for retrieving data from the form was more complicated than I thought:
从表单中检索数据的PHP比我想象的要复杂:
<h5>Select a gite:</h5>
<?php
$gitenames = array('giteA','giteB','giteC','giteD');
$gitename = isset($_POST['gitename']) && in_array($_POST['gitename'],$gitenames)?$_POST['gitename']:'giteA';
echo '<select name="gitename">';
foreach($gitenames as $option) {
echo '<option value="'.$option.'"'.(strcmp($option,$gitename)==0?' selected="selected"':'').'>'.$option.'</option>';
}
echo '</select>';
?>
<h5>Start date</h5>
<input type="date" class="form-control" name="start_date" value="<?php echo isset($start_date) ? set_value('start_date', date('Y-m-d', strtotime($start_date))) : set_value('start_date'); ?>">
<h5>End date</h5>
<input type="date" class="form-control" name="end_date" value="<?php echo isset($end_date) ? set_value('end_date', date('Y-m-d', strtotime($end_date))) : set_value('end_date'); ?>">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.