How to update database in codeigniter - php

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>

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.

Related

CakePHP - problem with merging add and edit

I am trying to "merge" add and edit function so to speak, into one "save" function, which will, whether $id of the model is provided, update or add a new instance of the model. By starting to code this, I realised that this idea brings more complications than built in CakePHP add and edit methods, thing is, my mentor insists that I merge this so I shall try it, even if i personally think this is not the best approach.
ItemTypesController.php
class ItemTypesController extends AppController {
public function save($id = null) {
if($this->request->is(array('post', 'put'))){
$this->ItemType->set($this->request->data);
//if i put an exit() funct here, it exits on this spot
if($this->ItemType->validates()){
if(!$id){
$this->ItemType->create();
if ($this->ItemType->save($this->request->data)) {
$this->Flash->success(__('The item type has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The item type could not be saved. Please, try again.'));
}
}
else{
if ($this->ItemType->save($this->request->data)) {
$this->Flash->success(__('The item type has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The item type could not be saved. Please, try again.'));
}
}
} else {
$this->Flash->warning($this->ItemType->validationErrors, array(
'key' => 'negative'
));
}
} else {
$options = array('conditions' => array('ItemType.' . $this->ItemType->primaryKey => $id));
$this->request->data = $this->ItemType->find('first', $options);
}
$this->set('classes', $this->ItemType->classes);
}
}
So basically the function will enter if block if $id is not provided, which is the case if i enter a link for a new ItemType. And it works fine for create, but, when I try to update it it always requires that any field must be changed, because of "isUnique" rule, i set that rule to be only for create, but CakePHP prolly thinks of its create function, albeit here is used my save() funct and it probably thinks it needs to enter that rule for my funct.
ItemType.php
class ItemType extends AppModel {
public $classes = array(
'product' => 'Proizvod',
'kit' => 'Kit (bundle)'
);
public $validate = array(
'code' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A code is required'
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'A code must be an alphanumeric value'
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This code already exists!',
'required' => 'create'
),
'between' => array(
'rule' => array('lengthBetween', 3, 7),
'message' => 'Code must be between 3 and 7 characters long'
)
),
'name' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A name is required'
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This name already exists!',
'required' => 'create'
),
'between' => array(
'rule' => array('lengthBetween', 3, 30),
'message' => 'Name must be between 3 and 30 characters long'
)
),
'class' => array(
'valid' => array(
'rule' => array('inList', array('product', 'material', 'kit', 'semi_product', 'service_product', 'service_supplier','consumable','inventory','goods','other')),
'message' => 'Please enter a valid class',
'allowEmpty' => false
)
),
'tangible' => array(
'bool' => array(
'rule' => 'boolean',
'message' => 'Incorrect value for the checkbox'
)
),
'active' => array(
'bool' => array(
'rule' => 'boolean',
'message' => 'Incorrect value for the checkbox'
)
)
);
public $hasMany = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'item_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
This is the view from which I am entering a new one or updating one:
index.ctp
<div class="itemTypes index">
<h2><?php echo __('Item Types'); ?></h2>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('code'); ?></th>
<th><?php echo $this->Paginator->sort('name'); ?></th>
<th><?php echo $this->Paginator->sort('class'); ?></th>
<th><?php echo $this->Paginator->sort('tangible'); ?></th>
<th><?php echo $this->Paginator->sort('active'); ?></th>
<th><?php echo $this->Paginator->sort('created'); ?></th>
<th><?php echo $this->Paginator->sort('modified'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($itemTypes as $itemType): ?>
<tr>
<td><?php echo h($itemType['ItemType']['id']); ?> </td>
<td><?php echo h($itemType['ItemType']['code']); ?> </td>
<td><?php echo h($itemType['ItemType']['name']); ?> </td>
<td><?php echo h($itemType['ItemType']['class']); ?> </td>
<td><?php if($itemType['ItemType']['tangible']) echo "Yes"; else echo "No" ?></td>
<td><?php if($itemType['ItemType']['active']) echo "Yes"; else echo "No" ?></td>
<td><?php echo h($itemType['ItemType']['created']); ?> </td>
<td><?php echo h($itemType['ItemType']['modified']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $itemType['ItemType']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'save', $itemType['ItemType']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $itemType['ItemType']['id']), array('confirm' => __('Are you sure you want to delete # %s?', $itemType['ItemType']['id']))); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?> </p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul><li><?php echo $this->Html->link(__('New Item Type'), array('action' => 'save')); ?>
</li>
</ul>
</div>
save.ctp
<div class="itemTypes form">
<?php echo $this->Form->create('ItemType'); ?>
<fieldset>
<legend><?php echo __('Add Item Type'); ?></legend>
<?php
echo $this->Form->input('code');
echo $this->Form->input('name');
echo $this->Form->input('class', array('options' => $classes));
echo $this->Form->input('tangible');
echo $this->Form->input('active');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions"><h3><?php echo __('Actions'); ?></h3><ul><li><?php echo $this->Html->link(__('List Item Types'), array('action' => 'index')); ?></li></ul></div>
Function save is not coded well, because id of the model was never set, so it did not knew whether it was updating or not.
public function save($id = null) {
if($this->request->is(array('post', 'put'))){
if($id){
$this->request->data['ItemType']['id'] = $id;
}
$this->ItemType->set($this->request->data);
if($this->ItemType->validates()){
if(!$id){
$this->ItemType->create();
}
if ($this->ItemType->save($this->request->data)) {
$this->Flash->success(__('The item type has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The item type could not be saved. Please, try again.'));
}
} else {
$this->Flash->warning($this->ItemType->validationErrors, array(
'key' => 'negative'
));
}
} else {
$options = array('conditions' => array('ItemType.' . $this->ItemType->primaryKey => $id));
$this->request->data = $this->ItemType->find('first', $options);
}
$this->set('classes', $this->ItemType->classes);}
Now, this is how function should look.

PHP error: array object converted into an associative array

I am making a CRUD application using Codeigniter 3.
Whenever I edit a record and the data is invalid, instead of just showing the validation error messages, the browser outputs the message: Trying to get property of non-object.
My update function inside the controller looks like this:
public function update($customer_id) {
// data validation
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
$this->form_validation->set_rules('phone', 'Phone number', 'required');
$this->form_validation->set_rules('country', 'Country', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$data = [
// insert into these database table fields
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'address' => $this->input->post('address')
];
$this->load->model('Customer');
if ($this->Customer->updateCustomer($customer_id, $data)) {
$this->session->set_flashdata('update-response','Customer successfully updated');
} else {
$this->session->set_flashdata('update-response','Failed to update customer');
}
redirect('home');
} else {
$data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'address' => $this->input->post('address'),
'id' => $customer_id
];
$this->load->view('update', ['customer' => $data]);
}
}
If the data in the update form is valid there are no errors. the form looks like this:
<?php echo form_open("home/update/{$customer->id}"); ?>
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<?php echo form_input('first_name', set_value('first_name', $customer->first_name),[
'id' => 'first_name',
'class' => 'form-control'
]);
if(form_error('first_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<?php echo form_input('last_name', set_value('last_name', $customer->last_name), [
'id' => 'last_name',
'class' => 'form-control'
]);
if(form_error('last_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<?php echo form_input('email', set_value('email', $customer->email), [
'id' => 'email',
'class' => 'form-control'
]);
if(form_error('email')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('phone')) echo 'has-error';?>">
<?php echo form_input('phone', set_value('phone', $customer->phone), [
'id' => 'phone',
'class' => 'form-control'
]);
if(form_error('phone')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('phone'); ?>
</div>
<div class="form-group <?php if(form_error('country')) echo 'has-error';?>">
<?php echo form_input('country', set_value('country', $customer->country), [
'id' => 'country',
'class' => 'form-control'
]);
if(form_error('country')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('country'); ?>
</div>
<div class="form-group">
<?php echo form_input('city', set_value('city', $customer->city), [
'id' => 'city',
'class' => 'form-control'
]);
?>
</div>
<div class="form-group">
<?php echo form_input('address', set_value('city', $customer->address), [
'id' => 'address',
'class' => 'form-control'
]);
?>
</div>
<div class="form-group">
<?php echo form_submit('submit', 'Save', 'class = "btn btn-success btn-block"'); ?>
</div>
<?php echo form_close(); ?>
I believe $customer is turned into an associative array and this is the reason for the error message: Trying to get property of non-object. But what is causing this conversion? Or is the cause of the problem completely different?
But what is causing this conversion?
Nothing, you pass an array and you get an array which you try to access like an object.
In your example, $data is an array, but in your view you are accessing it like an object.
If you really want to access it like an object, cast the array to object:
$this->load->view('update', ['customer' => (object)$data]);

CODEIGNITER: dynamically get drop down list but first to appear in list is the selected value

i have a table category that consist of (Dessert,Halal,Vegetarian). i was trying to create a CRUD. now i select the Dessert in my drop down list so i want to fetch all the value in my drop down but i want the Dessert to be seen first in dropdown. this is my code:
VIEW:
<?php $product_id = $product['recipe_id']; ?>
<?php echo form_open("dashboard/edit_product/$product_id");?>
<table>
<tr>
<td>Recipe Name: </td>
<td><?php echo form_input($r_name);?>
<div class="cleaner h10"></div>
</td>
</tr>
<tr>
<td>Recipe Category: </td>
<td><select name="r_category">
<option value=""><?php echo $r_category['value'];?></option>
</select>.
<div class="cleaner h10"></div>
</td>
</tr>
<tr>
<td>Recipe Description: </td>
<td><?php echo form_textarea($r_description); ?>
<div class="cleaner h10"></div></td>
</tr>
<tr>
<td>Recipe Cooking Time: </td>
<td><select name="cooking_time">
<option value=""><?php echo $cooking_time['value'];?></option>
</select>.
<div class="cleaner h10"></div>
</td>
</tr>
<tr>
<tr>
<td>Recipe Calories: </td>
<td><?php echo form_input($r_cal);?>
<div class="cleaner h10"></div>
</td>
</tr>
<tr>
<td>Recipe Serving Size: </td>
<td><?php echo form_input($r_serve);?>
<div class="cleaner h10"></div>
</td>
</tr>
<td>Recipe Procedure: </td>
<td><?php echo form_textarea($r_procedure); ?>
<div class="cleaner h10"></div></td>
</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() ?>dashboard'" />
</td>
</tr>
</table>
CONTROLLER:
function edit_product($product_id) {
$product = $this->products_model->get_product($product_id);
$this->load->model('products_model');
$this->data['title'] = 'Edit Recipe';
//validate form input
$this->form_validation->set_rules('r_name', 'Recipe name', 'required|xss_clean');
$this->form_validation->set_rules('r_description', 'Recipe description', 'required|xss_clean');
$this->form_validation->set_rules('r_cal', 'Recipe Calories', 'required|xss_clean');
$this->form_validation->set_rules('r_serve', 'Recipe Serving Size', 'required|xss_clean');
$this->form_validation->set_rules('r_procedure', 'Recipe procedure', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
$data = array(
'r_name' => $this->input->post('r_name'),
'r_category' => $this->input->post('r_category'),
'r_description' => $this->input->post('r_description'),
'cooking_time' => $this->input->post('cooking_time'),
'r_cal' => $this->input->post('r_cal'),
'r_serve' => $this->input->post('r_serve'),
'r_procedure' => $this->input->post('r_procedure'),
);
if ($this->form_validation->run() === true)
{
$this->products_model->update_product($product_id, $data);
$this->session->set_flashdata('message', "<p>Recipe updated successfully.</p>");
redirect(base_url().'dashboard/edit_product/'.$product_id);
}
}
$this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
$this->data['product'] = $product;
//display the edit product form
$this->data['r_name'] = array(
'name' => 'r_name',
'id' => 'r_name',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_name', $product['r_name']),
);
$this->data['r_image'] = array(
'name' => 'r_image',
'id' => 'r_image',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_image', $product['r_image']),
);
$this->data['r_category'] = array(
'name' => 'r_category',
'id' => 'r_category',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_category', $product['r_category']),
);
$this->data['r_description'] = array(
'name' => 'r_description',
'id' => 'r_description',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_description', $product['r_description']),
);
$this->data['cooking_time'] = array(
'name' => 'cooking_time',
'id' => 'cooking_time',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('cooking_time', $product['cooking_time']),
);
$this->data['r_cal'] = array(
'name' => 'r_cal',
'id' => 'r_cal',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_cal', $product['r_cal']),
);
$this->data['r_serve'] = array(
'name' => 'r_serve',
'id' => 'r_serve',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_serve', $product['r_serve']),
);
$this->data['r_procedure'] = array(
'name' => 'r_procedure',
'id' => 'r_procedure',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('r_procedure', $product['r_procedure']),
);
//var_dump($this->data);
$this->load->view('edit_product', $this->data);
}
model:
function get_product($product_id) {
$this->db->select('*');
$this->db->where('recipe_id', $product_id);
$query = $this->db->get('recipe');
return $query->row_array();
}

Editing and Deleting in Codeigniter

I created a table with search functionality that displays information from my database. I have made so on the last 2 columns of each row has an edit and delete link. The problem is that the when I click edit it won't direct me to the form I created and linked it to, As for the delete link it won't delete at all. I am going to include the code for the view, if you need me to show anymore code just ask and I'll do so.
<table align="center">
<tr>
<th>Voter #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Middle</th>
<th>Home #</th>
<th>Street</th>
<th>Apt</th>
<th>Zip</th>
<th>DOB</th>
<th>District</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
foreach($query as $voter){
$voter_id = $voter['voterNum'];
?>
<tr align="center">
<td><?php echo $voter['voterNum'] ?></td>
<td><?php echo $voter['firstName'] ?></td>
<td><?php echo $voter['lastName'] ?></td>
<td><?php echo $voter['midInitial'] ?></td>
<td><?php echo $voter['homeNum'] ?></td>
<td><?php echo $voter['street'] ?></td>
<td><?php echo $voter['apt'] ?></td>
<td><?php echo $voter['zip'] ?></td>
<td><?php echo $voter['dob'] ?></td>
<td><?php echo $voter['district'] ?></td>
<td>Edit</td>
<td><?php echo anchor('reg/delete_voter'.$voter_id, 'Delete',
array('onClick' => "return confirm('Are you sure you want to delete?')"));
?>
</td>
</tr>
<?php
}
?>
</table>
UPDATE
Controller
public function search(){
$search_term = array(
'firstName' => $this->input->post('firstName'),
'lastName' => $this->input->post('lastName'),
'street' => $this->input->post('street'),
'dob' => $this->input->post('dob')
);
$data['query'] = $this->reg_model->search_voters($search_term);
$this->load->view("reg_header");
$this->load->view("reg_nav");
$this->load->view("reg_search", $data);
}
function edit_voter($voter_id) {
$voter = $this->reg_model->get_voter($voter_id);
$this->data['title'] = 'Edit Voter';
$this->load->library("form_validation");
$this->form_validation->set_rules("firstName", "First Name", "required");
$this->form_validation->set_rules("lastName", "Last Name", "required");
$this->form_validation->set_rules("homeNum", "Home Number", "required");
$this->form_validation->set_rules("street", "Street", "required");
$this->form_validation->set_rules("zip", "Zip Code", "required");
$this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
$this->form_validation->set_rules("district", "District", "required");
if (isset($_POST) && !empty($_POST))
{
$data = array(
'firstName' => $this->input->post('firstName'),
'lastName' => $this->input->post('lastName'),
'midInitial' => $this->input->post('midInitial'),
'homeNum' => $this->input->post('homeNum'),
'street' => $this->input->post('street'),
'apt' => $this->input->post('apt'),
'zip' => $this->input->post('zip'),
'dob' => $this->input->post('dob'),
'district' => $this->input->post('district')
);
if ($this->form_validation->run() === true)
{
$this->reg_model->update_voter($voter_id, $data);
$this->session->set_flashdata('message', "<p>voter updated successfully.</p>");
redirect(base_url().'reg/search/'.$voter_id);
}
}
$this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
$this->data['voter'] = $voter;
$this->data['firstName'] = array(
'name' => 'firstName',
'id' => 'firstName',
'type' => 'text',
'value' => $this->form_validation->set_value('firstName', $voter['firstName'])
);
$this->data['lastName'] = array(
'name' => 'lastName',
'id' => 'lastName',
'type' => 'text',
'value' => $this->form_validation->set_value('lastName', $voter['lastName'])
);
$this->data['midInitial'] = array(
'name' => 'midInitial',
'id' => 'midInitial',
'type' => 'text',
'value' => $this->form_validation->set_value('midInitial', $voter['firstName'])
);
$this->data['homeNum'] = array(
'name' => 'homeNum',
'id' => 'homeNum',
'type' => 'text',
'value' => $this->form_validation->set_value('homeNum', $voter['homeNum'])
);
$this->data['street'] = array(
'name' => 'street',
'id' => 'street',
'type' => 'text',
'value' => $this->form_validation->set_value('street', $voter['street'])
);
$this->data['apt'] = array(
'name' => 'apt',
'id' => 'apt',
'type' => 'text',
'value' => $this->form_validation->set_value('apt', $voter['apt'])
);
$this->data['zip'] = array(
'name' => 'zip',
'id' => 'zip',
'type' => 'text',
'value' => $this->form_validation->set_value('zip', $voter['zip'])
);
$this->data['dob'] = array(
'name' => 'dob',
'id' => 'dob',
'type' => 'text',
'value' => $this->form_validation->set_value('dob', $voter['dob'])
);
$this->data['district'] = array(
'name' => 'district',
'id' => 'district',
'type' => 'text',
'value' => $this->form_validation->set_value('district', $voter['district'])
);
$this->load->view('edit_voter_form', $this->data);
}
function delete_voter($voter_id) {
$this->reg_model->del_voter($voter_id);
$this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');
redirect(base_url('reg/search'));
}
Why mix base_url() and anchor()? Just use anchor().
<td><?php echo anchor("reg/edit_voter/{$voter_id}", 'Edit') ?></td>
<td><?php echo anchor('reg/delete_voter/'.$voter_id, 'Delete',
array('onClick' => "return confirm('Are you sure you want to delete?')"));
?>
</td>
I think your delete link is missing a trailing slash.

PHP : combo box value is not getting updated in codeigniter

I have table view of some product which get data from database, there are two option edit and delete when I click on edit it shows particular data.
There is 2 categories inside combo box say Pizza and Sandwich.
By default Pizza is selected, and the value in database is also Pizza only now the problem begins, when I select sandwich instead of pizza the value in database is not getting updated.
I have used codeigniter framework for this project, so I want solution in same framework.
I have searched in google and many other way but hard luck, not getting proper solution what I want.
Any help would be appreciated. Thank in advance.
code: Controller
function products_edit($product_id)
{
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('products_model');
$data=$this->products_model->general();
$product = $this->products_model->get_product($product_id);
$category['categories']=$this->products_model->get_category();
$this->data1['title'] = 'Edit Product';
//validate form input
$this->form_validation->set_rules('name', 'Product name', 'required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
$this->form_validation->set_rules('is_featured', 'Is Featured', 'required|xss_clean');
$this->form_validation->set_rules('prorder', 'Order', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
$data1 = array(
'product_name'=> $this->input->post('name'),
'product_desc'=> $this->input->post('description'),
'product_category'=> $this->input->post('category'),
'extras'=> $this->input->post('extras'),
'price'=> $this->input->post('price'),
'is_featured'=> $this->input->post('is_featured'),
'prorder' => $this->input->post('order'),
);
if ($this->form_validation->run() === true)
{
$this->products_model->updateproducts($product_id, $data1);
$this->session->set_flashdata('message', "<p>Product updated successfully.</p>");
redirect('products_controller/products_edit/'.$product_id);
}
}
$this->data1['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
$this->data1['product'] = $product;
//$this->data1['category']=$category;
//display the edit product form
$this->data1['name'] = array(
'name' => 'name',
'id' => 'name',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('name', $product['product_name']),
);
$this->data1['description'] = array(
'name' => 'description',
'id' => 'description',
'type' => 'text',
'cols' => 40,
'rows' => 5,
'value' => $this->form_validation->set_value('description', $product['product_desc']),
);
$category1=$this->products_model->get_category();
$category1['value']=set_value('category',$product['product_category']);
$temp=array(
'1'=>$category1['value'],
);
$category1=array_diff($category1,$temp);
$category1['value']=set_value('category',$product['product_category']);
$this->data1['category']=$category1;
$this->data1['extras'] = array(
'value' => $this->form_validation->set_value('extras', $product['extras']),
);
$this->data1['price'] = array(
'name' => 'price',
'id' => 'price',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('price', $product['price']),
);
$this->data1['is_featured'] = array(
'name' => 'is_featured',
'id' => 'is_featured',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('isfeatured', $product['is_featured']),
);
$this->data1['prorder'] = array(
'name' => 'prorder',
'id' => 'prorder',
'type' => 'text',
'style' => 'width:40px;',
'value' => $this->form_validation->set_value('prorder', $product['prorder']),
);
$this->load->view('products_edit', $this->data1);
}
Model:
function get_product($product_id) {
$this->db->select('product_id,product_name,product_desc,product_category,extras,price,is_featured,prorder');
$this->db->where('product_id', $product_id);
$this->db->distinct('product_category');
$query = $this->db->get('product');
return $query->row_array();
}
function updateproducts($product_id, $data)
{
$this->db->where('product_id', $product_id);
$this->db->update('product', $data);
}
View :
<?php $product_id = $product['product_id']; ?>
<?php echo form_open("products_controller/products_edit/$product_id");?>
<table width="500" border="1" cellpadding="0" cellspacing="2" align="center">
<tr>
<td width="130" align="right"> Product Name: </td>
<td><?php echo form_input($name); ?></td>
</tr>
<tr>
<td width="130" align="right"> Product Description: </td>
<td><?php echo form_textarea($description); ?></td>
</tr>
<tr>
<td align="right">Product Category:</td>
<td>
<?php echo form_dropdown("product_id",$category,'value')?>
</td>
</tr>
<tr>
<td align="right">Extras:</td>
<td><?php echo form_dropdown("product_id",$extras,"#","id='product_id'");?></td>
</tr>
<tr>
<td align="right">Price:</td>
<td><?php echo form_input($price); ?></td>
</tr>
<tr>
<td align="right">Is Featured:</td>
<td><?php echo form_input($is_featured); ?></td>
</tr>
<tr>
<td align="right">Order:</td>
<td><?php echo form_input($prorder); ?></td>
</tr>
<tr>
<td> </td>
<td><?php echo form_submit('submit', 'Submit');?>
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<?php echo form_close(); ?>
<?php echo form_open("products_controller/products_search");?>
<?php echo form_submit('submit', 'Back');?>
<?php echo form_close(); ?>

Categories