使用 codeigniter 更新数据不起作用

Updating data not working using codeigniter

我目前正在使用 codeigniter 进行一个项目。作为项目功能的一部分,它具有编辑或更新功能,但我遇到了一些错误,它不是 working.Here 我的代码如下:

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: &nbsp;&nbsp;&nbsp; <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>

我现在的问题是,当我单击“更新”按钮时,它不起作用并且不更新 data.What 我应该怎么做? 任何想法或帮助表示赞赏。谢谢

使用名称属性作为 "worker_fname"

Name: &nbsp;&nbsp;&nbsp; <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

Your <input> field missing name attribute

所以最终井形代码

在视图中

<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: &nbsp;&nbsp;&nbsp; <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();
}