donglie7268 2015-10-14 08:43 采纳率: 100%
浏览 51
已采纳

使用codeigniter更新数据无效

I am currently working on a project using codeigniter. As part of the project functionalities, it have edit or update function and I got some errors with it, its not working.Here are my codes as follows :

Workers_model

This the edit function or update .

   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 (view)

<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>

My problem now is that when I click the Update button it's not working and does not update data.What should I do? Any ideas or help is appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • duanke2503 2015-10-14 08:56
    关注

    Your <input> field missing name attribute

    So final well form code is

    In View

    <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>
    

    In Controller

    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'
        }    
    
    }
    

    In Model

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀