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