我有两张桌子:
table: tbl_project
---------------------------------------
| PID | ProjectName | StartDate |
---------------------------------------
table: tbl_chart
-------------------------------------------------------------------------
| PID | P_ProjectPreparation | P_ConceptualDesign | P_Realization |
-------------------------------------------------------------------------
表tbl_project
中的PID是主键。 tbl_chart
中的ProjectID引用了tbl_project
中的PID。
我希望如果我点击保存以便将数据插入tbl_project
tbl_chart
也会自动插入新的PID,而P_ProjectPreparation
列的值等为0。
我已尝试过像这样的模型查询:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class admin_m extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function save($data)
{
$sql = "insert into tbl_project values('".$data['PID']."','".$data['ProjectName']."', '".$data['StartDate']."') ; insert into tbl_chart (PID) values ('".$data['PID']."')";
$this->db->query($sql);
}
}
这是我的控制器:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class admin_c extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->model('admin_m');
}
public function index()
{
$this->load->view('admin_v');
}
public function save()
{
$data['PID'] = $this->input->post('PID');
$data['ProjectName'] = $this->input->post('ProjectName');
$data['StartDate'] = $this->input->post('StartDate');
$this->admin_m->save($data);
$this->load->view('admin_v');
}
}
以下是我的保存按钮的视图代码:
<form action="<?PHP echo site_url(); ?>/admin_c/save" method="post">
我从模型中收到错误。任何想法查询可能有什么问题?
答案 0 :(得分:0)
你可以尝试做像
这样的事情application / controllers / Admin_c .php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class admin_c extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->model('admin_m');
}
public function index(){
$this->load->view('admin_v');
}
public function save(){
$this->form_validation->set_rules('PID', 'PID', 'trim|required|xss_clean');
$this->form_validation->set_rules('ProjectName', 'Project Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('StartDate', 'Date', 'trim|required|xss_clean');
if( $this->form_validation->run() === FALSE ){
print_r(validation_errors());
}
else{
$pid = $this->input->post('PID', TRUE);
$pname = $this->input->post('ProjectName', TRUE);
$date_s= $this->input->post('StartDate', TRUE);
$this->admin_m->save($pid, $pname, $date_s);
$this->load->view('admin_v');
}
}
}
应用/模型/ admin_m.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class admin_m extends CI_Model {
function __construct(){
parent::__construct();
}
public function save( $pid, $project_name, $start_date ){
$this->db->insert('tbl_project', array('ProjectName' => $project_name, 'StartDate' => $start_date));
$last_id = $this->db->insert_id();
$this->db->insert('tbl_chart', array('PID' => $last_id, 'P_ProjectPreparation' => NULL, 'P_ConceptualDesign' => NULL, 'P_Realization' => NULL));
}
}