我试图通过Codeigniter将数据插入数据库,但我无法继续。我有一个数据库mydatabase
,其中有5个表training_assignment_id
,assignment_name
,assignment_description
,recordings_required
和is_active
。 training_assignment_id
是自动递增的。
Controller-1 training.php
<?php
class Training extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper(array('form'));
$this->load->view('training_admin');
}
/*
function input()
{
$this->load->model('training');
if($this->input->post('mysubmit')){
$this->training->training_assignment_insert();
}
}*/
}
?>
Controller-2 add_training.php
<?php
class Add_training extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('training');
}
/*function index($assignment, $description, $check, $radio)
{
$assignment=$this->input->post('assignment');
$description=$this->input->post('assignment2');
$check=$this->input->post('assignment3');
$radio=$this->input->post('is_active');
//query the database
if($assignment!=NULL || $description!=NULL || $check!=NULL || $radio!=NULL)
{
$this->training->training_assignment_insert($assignment, $description, $check, $radio);
echo 'inserted successfully';
}
else {
echo "Data has not been inserted";
}
}*/
function index()
{
$this->training->training_assignment_insert(); // this should forward them to the Model
}
}
?>
模型 training.php
<?php
Class Training extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function training_assignment_insert($data ){
$data = array(
'assignment' => $this->input->post('assignment', TRUE),
'description' =>$this->input->post('assignment2', TRUE),
'check' => $this->input->post('assignment3', TRUE),
'radio' => $this->input->post('is_active', TRUE)
);
$this->db->insert('training_assignment', $data);
}
}
?>
查看 training_admin.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>resources/training/mystyle.css">
<title>Assignment</title>
</head>
<body>
<div id="container">
<h1>Add Assignment</h1>
<div id="body">
<?php echo form_open('traning/add_training'); ?>
<div id="label">
<input type="text" name="assignment" placeholder="Assignment Name" style="margin-left: 15px;"/>
<br/>
<textarea rows="4" cols="30" name="assignment2" placeholder="Assignment Description" style="margin-left: 15px;"></textarea>
<br/>
<label>Recording Require: </label>
<select name="assignment3">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
<br/>
<label for="active">Enable </label>
<input type="radio" name="is_active" id="enable" value="1"/>
<label for="female">Disable </label>
<input type="radio" name="is_active" id="disable" value="0"/>
<br/>
<input class="class1" type="submit" value="Assign"/>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</div>
</form>
</body>
</html>
请有人帮助我,我是CodeIgniter的新手。
答案 0 :(得分:0)
你需要将post值传递给model try 控制器
function index()
{
$data = array(
'assignment' => $this->input->post('assignment', TRUE),
'description' =>$this->input->post('assignment2', TRUE),
'check' => $this->input->post('assignment3', TRUE),
'radio' => $this->input->post('is_active', TRUE)
);
$this->training->training_assignment_insert($data); // this should forward them to the Model
}
和型号: -
function training_assignment_insert($data ){
$this->db->insert('training_assignment', $data);
}
答案 1 :(得分:0)
这样做
控制器
<?php
class Add_training extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('training');
}
function index()
{
$insert = array(
'assignment' => $this->input->post('assignment', TRUE),
'description' =>$this->input->post('assignment2', TRUE),
'check' => $this->input->post('assignment3', TRUE),
'radio' => $this->input->post('is_active', TRUE)
);
$this->training->training_assignment_insert($insert); // this should forward them to the Model
}
}
?>
模型
<?php
Class Training extends CI_Model
{
function __construct()
{
parent::__construct();
// $this->load->database();
}
function training_assignment_insert($data ){
$query = $this->db->insert('training_assignment', $data);
if($query){
return true;
}
}
}
?>