PHP : combo box value is not getting updated in codeigniter - php

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(); ?>

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.

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

Image validation in cakephp not working properly

I am trying to add image server side validation in cakephp but they are not working properly could anyone help me...i am new in cakephp...here is my view and model file code:-
I am using custom plugin name "Account"
Account.php
class Account extends AccountAppModel {
public $useTable = 'users';
public $name = "Account";
public $validate = array(
'name' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your name!'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Name must be alphabetic or number!'
),
'between' => array(
'rule' => array('between', 3, 20),
'on' => 'create',
'message' => 'Name must be between 5to20 character!'
),
'isUnique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Name already taken! please choose another-one!'
)
),
'email'=>array(
'notEmpty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter Mail ID!'
),
'email'=>array(
'rule'=>'email',
'message'=>'Please enter a valid mail id!'
)
),
'region'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter Region!'
),
'city'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter City!'
),
'password'=>array(
'notEmpty'=>array(
'rule'=>'notEmpty',
'message'=>'please enter password'
),
'between'=>array(
'rule'=>array('between', 5,20),
'message'=>'password must be between 5 to 20 character'
)
),
'cpassword'=>array(
'compare' => array(
'rule'=>array('confirmPassword'),
'message' => 'Password do not match please try again!'
)
),
'photo'=>array(
'mimeType'=>array(
'rule'=>array('mimeType',"photo","photo"),
'message'=>'Please enter a valid image!'
),
'fileSize'=>array(
'rule' => array('fileSize', '<=', '1MB'),
'message'=>'Image is too big please try again!'
),
'uploadError'=>array(
'rule'=>'uploadError',
'message'=>'File upload field!'
)
),
'photo_id'=>array(
'mimeType'=>array(
'rule'=>array('mimeType','photo_id','photo_id'),
'message'=>'Please enter a valid image!'
),
'fileSize'=>array(
'rule'=>array('fileSize','<=','1MB'),
'message'=>'Image is too big please try again!'
),
'uploadError'=>array(
'rule'=>'uploadError',
'message'=>'File upload field!'
)
)
);
function confirmPassword()
{
return $this->data['Account']['password']==$this->data['Account']['cpassword'];
}
public function mimeType($field = null,$cfield = null){
//pr($cfield) or die;
if(isset($field[$cfield]["tmp_name"]) && !empty($field[$cfield]["tmp_name"]))
{
$size = #getimagesize($field[$cfield]["tmp_name"]);
//pr($size) or die;
$imgType = array('image/jpeg','image/jpg');
if(in_array($size['mime'],$imgType)){
$return = true;
}
else{
$return = false;
}
return $return;
}
}
}
and here is add.ctp file code:-
<?php echo $this->Form->create('Account',array('type'=>"file")); ?>
<table width='800px' border='0' align='center'>
<tr>
<td>Name :</td>
<td>
<?php
echo $this->Form->input('name',array('label'=>false,'data-rule-required'=>true,'lettersonly'=>true,"required"=>false));
?>
</td>
<td>E-Mail :</td>
<td>
<?php
echo $this->Form->input('email',
array('label'=>false,'required'=>false,'type'=>'text'));
?>
</td>
</tr>
<tr>
<td>Region :</td>
<td>
<?php
echo $this->Form->input('region',
array('type'=>'select',
'empty'=>'Select Region',
'options'=>$strlt,
'label'=>false,
'id'=>'name',
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>City :</td>
<td>
<?php
echo $this->Form->input('city',
array('type'=>'select',
'empty'=>'Select City',
'options'=>'',
'label'=>false,
'id'=>'slct',
'data-rule-required'=>true,'required'=>false
)
);
?>
</td>
</tr>
<tr>
<td>Password :</td>
<td>
<?php
echo $this->Form->input('password',
array('type'=>'password',
'label'=>false,
'data-rule-required'=>true,
'id'=>'pass_id',
'required'=>false));
?>
</td>
<td>Confirm Password :</td>
<td>
<?php
echo $this->Form->input('cpassword',
array('type'=>'password',
'label'=>false,
'data-rule-required'=>true,
'equalTo'=>'pass_id','required'=>false));
?>
</td>
</tr>
<tr>
<td>Address</td>
<td>
<?php
echo $this->Form->input('address',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>Zip-Code :</td>
<td>
<?php
echo $this->Form->input('zip',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr>
<td>About Me :</td>
<td>
<?php
echo $this->Form->textarea('about_me',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>Contact No :</td>
<td>
<?php
echo $this->Form->input('contact_no',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr>
<td>Upload Photo :</td>
<td>
<?php
echo $this->Form->input('photo',
array('type'=>'file',
'label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>Photo ID :</td>
<td>
<?php
echo $this->Form->input('photo_id',
array('type'=>'file',
'label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr>
<td>Show Info :</td>
<td>
<?php
echo $this->Form->input('show_info',
array('type'=>'checkbox',
'label'=>false));
?>
</td>
<td>Premium Member :</td>
<td>
<?php
echo $this->Form->input('membership',
array('type'=>'checkbox',
'label'=>false,'required'=>false));
?>
</td>
</tr>
<tr>
<td>Unit / Company :</td>
<td>
<?php
echo $this->Form->input('user_type', array(
'type' => 'select',
'options' => array('unit' => 'Unit', 'company' => 'Company'),
'selected' => 'Unit',
'label'=>false,
'data-rule-required'=>true,'required'=>false
));
?>
</td>
<td>Status :</td>
<td>
<?php
echo $this->Form->input('status',
array('type'=>'select',
'options'=>array('1'=>'Active',
'0'=>'Inactive'),
'label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr><td colspan=2>
<?php echo $this->Form->input('submit',
array('type'=>'submit',
'label'=>false,'class'=>'btn btn-default','id'=>'sb'));
?>
<?php
echo $this->Html->link('Cancel',
array('plugin'=>'account',
'controller'=>'accounts',
'action'=>'index'),
array('class'=>'btn btn-primary',
'div'=>false,
'escape'=>false,
'id'=>'sb'
)
);
?>
<td><tr>
</table>
<?php echo $this->Form->end();?>
Use Rule ,(Replace your image rule with this)
'photo_id'=>array(
'mimeType'=>array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message'=>'Please enter a valid image!'
),

How to update database in codeigniter

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.

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.

Categories