Having a nightmare here and can't work out what's wrong. Basically I have a codeigniter setup that is running a couple of database queries but for some reason it doesn't seem to work the first time after a refresh.
So this is what is currently happening: I submit the form and the form data goes in to the database correctly, however the $property_id ends up blank in createInitialTenancy() function and the $tenancy_id ends up blank in the insertBasicInfo() function.
If I then submit the form again (either by submitting without a refresh or by refreshing) then both these values are populated.
Can anyone explain a reason behind this?
Controller:
public function new($property_id) {
// Check for a property ID. Redirect to dashboard if none present.
if(!$property_id) {
redirect(base_url());
}
// Check for the form having been submitted
if ($this->input->post()) {
// Strip form data for XSS
$form_data = $this->security->xss_clean($this->input->post());
// Create the initial tenancy entry in the database
$tenancy_data['property_id'] = $property_id;
$tenancy_id = $this->tenancy_model->createInitialTenancy($tenancy_data);
// Empty tenant array. We've dealt with this elsewhere
unset($form_data['tenant']);
// Insert the data to the tenancy.basic_info database
$form_data['lead_tenant_id'] = $_SESSION["tenant_id"];
$form_data['property_id'] = $property_id;
$form_data['tenancy_id'] = $tenancy_id;
$this->tenancy_model->insertBasicInfo($form_data);
}
// Security token required for the form
$csrf = array(
'name' => $this->security->get_csrf_token_name(),
'hash' => $this->security->get_csrf_hash()
);
$this->load->view('header-signin');
$this->load->view('tenant/tenancy/new', $csrf);
$this->load->view('footer-signin');
}
Model:
class Tenancy_model extends CI_Model {
function __construct() {
$this->load->database();
parent::__construct();
}
public function insertBasicInfo($form_data) {
$this->db->insert('tenancy_basic_info', $form_data);
}
public function createInitialTenancy($data) {
$this->db->insert('tenancy_tenancy', $data);
return $this->db->insert_id();
}
public function getTenancyByPropertyID($property_id) {
$this->db->select('id');
$this->db->from('tenancy_tenancy');
$this->db->where('property_id', $property_id);
$tenancy = $this->db->get()->result();
return $tenancy;
}
}
tenancy_tenancy Table:
CREATE TABLE `tenancy_tenancy` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`property_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8;
tenancy_basic_info Table:
CREATE TABLE `tenancy_basic_info` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`tenancy_id` int(11) DEFAULT NULL,
`lead_tenant_id` int(11) NOT NULL,
`property_id` int(11) NOT NULL,
`more-tenants` int(11) NOT NULL,
`pets` int(11) NOT NULL,
`pets_info` text,
`cars` int(11) NOT NULL,
`number_of_cars` int(11) NOT NULL,
`information` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;