简体   繁体   English

如何使用Codeigniter编辑记录

[英]How Edit record using Codeigniter

I am trying to edit my db record using codeigniter but I can't do it and what's the problem I didn't understand. 我正在尝试使用codeigniter编辑我的数据库记录,但我无法做到这一点,我不明白的问题是什么。 Please help. 请帮忙。

Here is my controller code : 这是我的控制器代码:

    public function edit()
    {
        $this->form_validation->set_rules('name','Name','trim|required');
        $this->form_validation->set_rules('email','E-mail','trim|required');
        $this->form_validation->set_rules('phone','Phone','trim|required');

        if ($this->form_validation->run() == FALSE) {
            echo "error";
        }
        else{
            $id = $this->input->post('id');
            $this->crud_mdl->editCrud($id);
            echo "success";
        }
    }

Here is my model code : 这是我的型号代码:

    public function editCrud($id)
    {
        $update = array(
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone')
            );
        $this->db->where('id',$id);
        return $this->db->update('test', $update);
    }

Here is my view : 这是我的观点

    <div>
    <?php echo form_open('crud/edit');?>
    <?php foreach ($records as $record) {?>
    <label> ID </label>
    <input type="text" name="id" id="id" disabled="disable" value = "<?php echo $record->id ;?>" /><br/>
    <label>Name</label>
    <input type="text" name="name" id ="name" value = "<?php echo $record->name ;?>" /><br/>
    <label>E-Mail</label>
    <input type="text" name="email" id ="email" value="<?php echo $record->email ;?>" /><br/>
    <label>Phone</label>
    <input type="text" name="phone" id ="phone" value = "<?php echo $record->phone ;?>" /><br/>
    <input type="submit" name="edit" value="Edit" />
    <?php }?>
    <?php echo form_close();?>
</div>

You have disabled="disable" in your input for id . 您已在inputid disabled="disable" This means it won't be included in the form POST . 这意味着它不会包含在POST表单中。 You need to remove disabled="disable" . 你需要删除disabled="disable"

Try that: 试试看:

<label> ID </label>
<input type="text" name="id" id="id" value="<?php echo $record->id ;?>" />

For proper practices you should set the input type to hidden instead or put the ID in the url (depends on your application). 对于正确的实践,您应该将输入类型设置为hidden或者将ID放在URL中(取决于您的应用程序)。

<input type="hidden" name="id" id="id" value="<?php echo $record->id ;?>" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM