我目前正在使用codeigniter开展项目。作为项目功能的一部分,它有编辑或更新功能,我有一些错误,它不工作。这是我的代码如下:
Workers_model
这是编辑功能或更新。
public function edit_workers_details ($worker_id, $data) {
$this->db->where('worker_id', $worker_id);
$this->db->update('worker_tb', $data);
return $this->db->affected_rows();
}
public function get_worker_details ($worker_id) {
$this->db->select();
$this->db->from('worker_tb');
$this->db->where('worker_id', $worker_id);
$query = $this->db->get();
return $query->result_array();
}
**Workers (controller)**
public function edit_worker ($worker_id) {
if ($_POST) {
$worker_details = array (
'worker_fname' => $_POST['worker_fname']
);
$this->Workers_model->edit_workers_details($worker_id, $worker_details);
redirect("Workers/index");
}
$data['details'] = $this->Workers_model->get_worker_details($worker_id);
$this->load->view('Worker/updateWorker',$data);
}
updateWorker(查看)
<center>
<h1> Edit Worker Details </h1>
<?php foreach ($details as $detail) {?>
<form role="form" action= "<?php echo site_url("Workers/edit_worker/".$detail['worker_id']);?>" method="POST">
Name: <input type="text" class="form-control" value = "<?php echo $detail['worker_fname'];?>" ><br />
<button type="submit" class="btn btn-primary"> Update </button>
</form>
<?php } ?>
</center>
我现在的问题是,当我点击“更新”按钮时,它无法正常工作,也不会更新数据。我该怎么办? 任何想法或帮助表示赞赏。感谢
答案 0 :(得分:0)
使用name属性作为&#34; worker_fname&#34;
Name: <input type="text" name ="worker_fname"class="form-control" value = "<?php echo $detail['worker_fname'];?>" ><br />
然后你可以从控制器获得价值形式价值像这样
$this->input->post("worker_fname"); or $_POST['worker_fname']
然后做你想做的事:D:D
答案 1 :(得分:0)
您的
<input>
字段缺少name
属性
所以最终井形式代码是
在视图中
<center>
<h1> Edit Worker Details </h1>
<?php foreach ($details as $detail) {?>
<form role="form" action= "<?php echo site_url("Workers/edit_worker/".$detail['worker_id']);?>" method="POST">
Name: <input type="text" name="worker_fname" class="form-control" value = "<?php echo $detail['worker_fname'];?>" ><br />
<button type="submit" class="btn btn-primary"> Update </button>
</form>
<?php } ?>
</center>
在控制器中
public function edit_worker ($worker_id) {
$worker_fname = $_POST['worker_fname'];
if(!empty($worker_fname))
{
$result = $this->Workers_model->edit_workers_details($worker_id, $worker_fname);
if($result == 1)
{
$data['details'] = $this->Workers_model->get_worker_details($worker_id);
$this->load->view('Worker/updateWorker',$data);
}
else
{
echo 'Update Failed'
}
}
else
{
echo 'worker_fname is empty'
}
}
在模型中
public function edit_workers_details ($worker_id, $worker_fname) {
$data = array(
'worker_fname' => $worker_fname
);
$this->db->where('worker_id', $worker_id);
if(!$this->db->update('worker_tb', $data))
{
// Update Failed
return $log = 0;
}
else
{
// Update Success
return $log = 1;
}
}
public function get_worker_details ($worker_id)
{
$this->db->select();
$this->db->from('worker_tb');
$this->db->where('worker_id', $worker_id);
$query = $this->db->get();
return $query->result_array();
}