我有 2 张 table :
table: tbl_project
---------------------------------------
| PID | ProjectName | StartDate |
---------------------------------------
table: tbl_chart
-------------------------------------------------------------------------
| PID | P_ProjectPreparation | P_ConceptualDesign | P_Realization |
-------------------------------------------------------------------------
表中的PID tbl_project
是主键。以及 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);
}
}
这是我的 Controller :
<?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">
我从模型中收到错误。您知道查询可能出现什么问题吗?
最佳答案
你可以尝试做类似的东西
应用程序/ Controller /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');
}
}
}
应用程序/models/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));
}
}
关于php - 使用 php 和 codeigniter 将数据插入到两个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645317/