gpt4 book ai didi

php - 无法使用 CodeIgniter 插入数据库

转载 作者:行者123 更新时间:2023-11-30 23:02:22 27 4
gpt4 key购买 nike

我正在尝试通过 Codeigniter 将数据插入数据库,但无法继续。我有一个数据库 mydatabase,其中包含 5 个表 training_assignment_idassignment_nameassignment_descriptionrecordings_requiredis_activetraining_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 的新手。

最佳答案

这样做

Controller

  <?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;
}
}
}
?>

关于php - 无法使用 CodeIgniter 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23407172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com