簡體   English   中英

如何在Codeigniter中更新數據庫

[英]How to update database in codeigniter

我有用於更新數據庫的這段代碼,但無法正常工作。 幫助我跟蹤錯誤:錯誤是在驗證中,但是我的代碼找不到任何問題。 它帶來以下錯誤:'product_name required''username required''email required''usertype required',因此它不會更新我的數據庫。 我剛剛在控制器和視圖中僅包含了4個項目的代碼

我的控制器

function edit_product($product_id) {
    $product = $this->generalmod_model->get_product($product_id);

    //validate form input
    $this->form_validation->set_rules('product_name', 'product_name name', 'required|xss_clean');
    $this->form_validation->set_rules('username', 'username', 'required|xss_clean');
    $this->form_validation->set_rules('email', 'email', 'required|xss_clean');
    $this->form_validation->set_rules('usertype', 'usertype', 'required|xss_clean');

    if (isset($_POST) && !empty($_POST))
    {       
        $data = array(
            'product_name'      => $this->input->post('product_name'),
            'username'  => $this->input->post('username'),
            'email'     => $this->input->post('email'),
            'usertype'  => $this->input->post('usertype'),
        );

        if ($this->form_validation->run() === true)
        {
            $this->generalmod_model->update_product($product_id, $data);
            echo "updated successfully";
        }           
    }

    $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

    $this->data['product'] = $product;

    //display the edit product form
    $this->data['product_name'] = array(
        'product_name'      => 'product_name',
        'id'        => 'product_name',
        'type'      => 'text',
        'style'     => 'width:300px;',
        'value'     => $this->form_validation->set_value('product_name', $product['product_name']),
    );

    $this->data['username'] = array(
        'username'      => 'username',
        'id'        => 'username',
        'type'      => 'text',
        'style'         => 'width:300px',
        'value'     => $this->form_validation->set_value('username', $product['username']),
    );

    $this->data['email'] = array(
        'email'     => 'email',
        'id'        => 'email',
        'type'      => 'text',
        'style'     => 'width:300px',
        'value'     => $this->form_validation->set_value('email', $product['email']),
    );

    $this->data['usertype'] = array(
        'usertype'  => 'usertype',
        'id'    => 'usertype',
        'type'  => 'text',
        'style' => 'width:300px;',
        'value' => $this->form_validation->set_value('usertype', $product['usertype']),
    );

    $this->load->view('update_profile', $this->data);
}

我的模特

function get_product($product_id) 
{
    $this->db->select('user_id, product_name, username, email, usertype');
    $this->db->where('user_id', $product_id);
    $query = $this->db->get('users');

    return $query->row_array();
}

public function update_product($product_id, $data)
{
    $this->db->where('user_id', $product_id);
    $this->db->update('users', $data);
}

查看:

echo form_open("generalcon/edit_product/$product_id");?>
<table width="700" border="1" cellpadding="0" cellspacing="2" align="center">
    <tr>
        <td width="130" align="right"> product Name: </td>
        <td><?php echo form_input($product_name); ?></td>
    </tr>
    <tr>
        <td width="130" align="right">  username: </td>
        <td><?php echo form_input($username); ?></td>
    </tr>
    <tr>
        <td align="right">Email:</td>
        <td><?php echo form_input($email); ?></td>
    </tr>
    <tr>
        <td align="right">usertype:</td>
        <td><?php echo form_input($usertype); ?></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><?php echo form_submit('submit', 'Submit');?>
            <input type="button" name="btnBack" id="btnBack" value="Back" onclick="window.location.href='<?php echo base_url() ?>manage'" />
        </td>
    </tr>
</table>

對於所收到的驗證錯誤,在設置表單字段的控制器中的每個數組中,數組鍵都必須是“名稱”而不是其標題。 例如,您有'username' => 'username''email' => 'email' 這需要是'name' => 'username''name' => 'email'

另外,在視圖文件的第一行中,php並未按預期解析$product_id變量。 您需要<?php echo form_open("generalcon/edit_product/".$product_id); ?> <?php echo form_open("generalcon/edit_product/".$product_id); ?> ,其中$ product_id用引號引起來,並用句點連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM