[英]Insert data into two tables using php and codeigniter
我有2张桌子:
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">
我从模型中得到一个错误。 任何想法查询可能出什么问题吗?
你可以尝试做类似的东西
应用程序/控制器/ 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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.