我刚刚开始学习CodeIgniter
。但是我坚持了一点。经过7个多小时的工作和谷歌搜索,我决定在这里询问。我正在尝试更新数据库,但无法摆脱。输入数据后,我的数据不会更新。这是我的代码;
Takvim_Model.php
function __construct() {
parent::__construct();
}
public function insert($data) {
if ($this->db->insert('takvim', $data)) {
return true;
}
}
public function delete($roll_no) {
if ($this->db->delete('takvim', 'roll_no = '.$roll_no)) {
return true;
}
}
public function update($data,$old_roll_no) {
$this->db->set($data);
$this->db->where('roll_no', $old_roll_no);
$this->db->update('takvim', $data);
}
}
?>
Takvim_View.php
<!DOCTYPE html> <html lang = "en"> <head>
<meta charset = "utf-8">
<title>Takvim Example</title> </head> <body>
<a href = "<?php echo base_url(); ?>index.php/takvim/add_view">Add</a>
<table border = "1">
<?php
$i = 1;
echo "<tr>";
echo "<td>Sr#</td>";
echo "<td>Roll No.</td>";
echo "<td>Name</td>";
echo "<td>Start Time</td>";
echo "<td>Stop Time</td>";
echo "<td>Update</td>";
echo "<td>Delete</td>";
echo "<tr>";
foreach($records as $r) {
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$r->roll_no."</td>";
echo "<td>".$r->name."</td>";
echo "<td>".$r->start_time."</td>";
echo "<td>".$r->stop_time."</td>";
echo "<td><a href = '".base_url()."index.php/takvim/update/"
.$r->roll_no."'>Update</a></td>";
echo "<td><a href = '".base_url()."index.php/takvim/delete/"
.$r->roll_no."'>Delete</a></td>";
echo "<tr>";
}
?>
</table>
Takvim_Controller.php
<?php
class Takvim_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("takvim");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Takvim_view', $data);
}
public function add_event_view() {
$this->load->helper('form');
$this->load->view('Takvim_add');
}
public function add_event() {
$this->load->model('Takvim_model');
$data = array (
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name'),
'start_time' => $this->input->post('start_time')
);
$this->Takvim_model->insert($data);
$query = $this->db->get("takvim");
$data['records'] = $query->result();
$this->load->view('Takvim_view',$data);
}
public function update_event_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("takvim",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data = array('records' => $data['records']);
$this->load->view('Takvim_update',$data);
}
public function update_event(){
$this->load->model('Takvim_Model');
$data = array (
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name'),
'start_time' => $this->input->post('start_time'),
'stop_time' => $this->input->post('stop_time')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Takvim_Model->update($data,$old_roll_no);
$query = $this->db->get("takvim");
$data['records'] = $query->result();
$this->load->view('Takvim_view',$data);
}
public function delete_event() {
$this->load->model('Takvim_Model');
$roll_no = $this->uri->segment('3');
$this->Takvim_Model->delete($roll_no);
$query = $this->db->get("takvim");
$data['records'] = $query->result();
$this->load->view('Takvim_view',$data);
}
} ?>
Takvim_Update.php
<!DOCTYPE html><html lang = "en"> <head> <meta charset = "utf-8">
<title>Takvim Example</title> </head> <body>
<form method = "" action = "">
<?php
echo form_open('Takvim_controller/update');
echo form_hidden('old_roll_no',$records[0]->roll_no);
echo form_label('Roll No.');
echo form_input(array('id'=>'roll_no',
'name'=>'roll_no','value'=>$records[0]->roll_no));
echo "
";
echo form_label('Name');
echo form_input(array('id'=>'name','name'=>'name',
'value'=>$records[0]->name));
echo "
";
echo form_label('Start Time');
echo form_input(array('id'=>'name','name'=>'name',
'value'=>$records[0]->start_time));
echo "
";
echo form_label('Stop Time');
echo form_input(array('id'=>'name','name'=>'name',
'value'=>$records[0]->stop_time));
echo "
";
echo form_submit(array('id'=>'submit','value'=>'Edit'));
echo form_close();
?>
</form>
Routes.php
$route['takvim'] = "Takvim_controller";
$route['takvim/add'] = 'Takvim_controller/add_event';
$route['takvim/add_view'] = 'Takvim_controller/add_event_view';
$route['takvim/update/(\d+)'] = 'Takvim_controller/update_event_view/$1';
$route['takvim/delete/(\d+)'] = 'Takvim_controller/delete_event/$1';
答案 0 :(得分:0)
Codeigniter的型号名称不建议
当您命名模型时,请使用_model
而不是_Model
(此创建限制在您的代码中)
答案 1 :(得分:0)
在Takvim_Update.php
中,您正在向'Takvim_controller/update'
提交表单,但在您的update
或您的Takvim_controller
路线中看不到Takvim_controller/update
方法routes.php
文件。
首先编辑您的Routes.php
,然后在Takvim_controller
中向您的更新方法添加新路线。
$route['takvim/update'] = 'Takvim_controller/update_event';
然后在Takvim_Update.php
文件中将您的发件人提交到takvim/update
网址。
echo form_open('takvim/update');
答案 2 :(得分:0)
尝试一下:希望对您有所帮助。 定义路线。
$route['takvim/update/(:any)'] = 'Takvim_controller/update_event_view/$1';
//控制器 首先从表格中获取数据。然后根据您要更新的参数进行更新。例如,我要提供表的主键ID。
$data=array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name'),
'start_time' => $this->input->post('start_time'),
'stop_time' => $this->input->post('stop_time')
);
$this->Takvim_Model->update($id);
//型号
function update($id){
return $this->db->where('id',$id)->update('table_name',$data);
}