PHP数据库查询 - 没有第一次工作(CodeIgniter)

时间:2016-10-18 11:38:19

标签: php mysql codeigniter

在这里做一场噩梦,无法解决错误。基本上我有一个运行几个数据库查询的codeigniter设置但由于某些原因它在刷新后似乎没有第一次工作。

所以这就是目前正在发生的事情:我提交表单并且表单数据正确地进入数据库,但$ property_id在createInitialTenancy()函数中结束为空,$ tenancy_id在insertBasicInfo()中结束为空功能

如果我再次提交表单(通过提交而不刷新或刷新),则会填充这两个值。

有谁可以解释这背后的原因?

控制器:

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');
}

型号:

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表:

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表:

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;

0 个答案:

没有答案
相关问题