0

I have this code for updating the database but wont just work. Help me to trace the error: The error is in validation but i cant find any problem with my code. It brings this error: 'product_name required' 'username required ' 'email required' 'usertype required' and therefore it does not update my database. I have just included code for only 4 items in the controller and view

My controller

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);
}

My model

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);
}

view :

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>
1
  • Open browser with this form and post .HTML source here. Commented Apr 21, 2014 at 15:56

1 Answer 1

1

For the validation error that you are getting, in each of the arrays in the controller where you set up the form fields, the array key needs to be 'name' instead of its title. For example you have 'username' => 'username' and 'email' => 'email'. This needs to be 'name' => 'username' and 'name' => 'email'.

Also, in the first line of the view file, the $product_id variable is not being parsed by php as you intend. You need <?php echo form_open("generalcon/edit_product/".$product_id); ?> with the $product_id outside the quotation marks and concatenated with a period.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.