how to properly update a row in a database using codeigniter - php

I am not sure where i went wrong here but please bear with me, this is the url that comes up after click on the save button, http://localhost/gabbyville/tenant/contact/update/contact/save all want to happen, is to change the existing data on a row from a database. can anyone please help me?
here is the controller
public function update($id)
{
$contact['contact'] = $this->Contacts_model->get_all();
$contacts['contacts'] = $this->Contacts_model->get_by_id($id);
$data['content'] = $this->load->view("tenant/contacts",$contacts,TRUE);
$data['content'] = $this->load->view("tenant/contacts",$contact,TRUE);
$data['curpage'] = $this->curpage;
$this->load->view('template1',$data);
}
public function save($id)
{
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'postal_address' => $this->input->post('postal_address'),
'company_name' => $this->input->post('company_name'),
'company_url' => $this->input->post('company_url'),
'industry' => $this->input->post('industry'),
'phone_number' => $this->input->post('phone_number'),
);
$this->Contacts_model->update($this->uri->segment(), $data);
redirect($this->view_folder.$this->curpage, 'refresh');
}
here is the model
function get_by_id($id)
{
$this->db->where($this->id, $id);
return $this->db->get($this->table)->row();
}
function update($id, $data)
{
$this->db->where($this->id, $id);
$this->db->update($this->table, $data);
}
here is the form from my view
<form action="contact/save" method="post">
<div class="row ">
<input type="text" class="form-control mBottom2 mTop2" name="first_name" placeholder="First Name" value="<?php echo $contacts->first_name?>">
<input type="text" class="form-control mBottom2" name="last_name" placeholder="Last Name" value="<?php echo $contacts->last_name?>">
<input type="text" class="form-control mBottom2" name="postal_address" placeholder="Postal Address" value="<?php echo $contacts->postal_address?>">
<input type="text" class="form-control mBottom2" name="company_name" placeholder="Company Name" value="<?php echo $contacts->company_name?>">
<input type="text" class="form-control mBottom2" name="company_url" placeholder="Company URL" value="<?php echo $contacts->company_url?>">
<input type="text" class="form-control mBottom2" name="industry" placeholder="Industry" value="<?php echo $contacts->industry?>">
<ul class="row contact">
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" name="phone_number" placeholder="Phone Number" value="<?php echo $contacts->phone_number?>">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
</li>
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" placeholder="Enter New Phone Number">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
<a><i class="fa fa-trash-o"></i></a>
</li>
<li class="">
<button type="submit" class="btn btn-w-m btn-primary">Add New</button>
</li>
</ul>
</form>
<ul class="row contact">
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" placeholder="Email">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
</li>
<li class="mBottom2 clearfix">
<input type="text" class="form-control pull-left" placeholder="Enter New Email">
<div class="i-checks pull-left">
<label><input type="checkbox"><i></i></label>
</div>
<a><i class="fa fa-trash-o"></i></a>
</li>
<li class="mBottom2">
<button type="button" class="btn btn-w-m btn-primary">Add New</button>
</li>
</ul>
</div>

Use only one model to update or insert the data into the table:
Your Model should be like this:
public function save($data = array())
{
if(isset($data['id']) && ((int)$data['id'] > 0))
{
$this->db->where('id', (int)$data['id']);
$this->db->update('table_name', $data);
$id = $data['id'];
}
else
{
$this->db->insert('table_name', $data);
$id = $this->db->insert_id();
}
return $id;
}
And send values from controller to model like this:
$data = array(
'id' => ($this->input->post('id')) ? $this->input->post('id') : 0,
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'postal_address' => $this->input->post('postal_address'),
'company_name' => $this->input->post('company_name'),
'company_url' => $this->input->post('company_url'),
'industry' => $this->input->post('industry'),
'phone_number' => $this->input->post('phone_number'),
);

Related

Crud in Codeigniter not affected when Update to database

I've been working to make some CRUD I get nothing wrong.
But when I update data I got nothing affected in database and here my code
Table : Song
Column :
id_song,
id_album,
song,
author,
composer
Code for my Model is below:
function Tampil_song_admin(){
return $this->db->get('songs');
}
function Input_song($data,$table){
$this->db->insert($table,$data);
}
function edit_song($where,$table){
return $this->db->get_where($table,$where);
}
function update_song($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
function delete_song($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
View:
<?php foreach($songs as $data){ ?>
<form action="<?php echo base_url('/index.php/Admin_song/update_song'); ?>" method="post">
<h4 class="header-title m-t-0 m-b-30"></h4>
<div class="form-group">
<label for="userName">id_songs </label>
<input type="text" name="id_song" parsley-trigger="change" required
value="<?php echo $data->id_song; ?>"
class="form-control" disabled="" id="userName">
</div>
<div class="form-group">
<label for="userName">id_album</label>
<input type="text" name="id_album" parsley-trigger="change" required
value="<?php echo $data->id_album; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Songs</label>
<input type="text" name="song" parsley-trigger="change" required
value="<?php echo $data->song; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Author</label>
<input type="text" name="author" parsley-trigger="change" required
value="<?php echo $data->author; ?>" class="form-control" id="userName">
</div>
<div class="form-group">
<label for="userName">Composer</label>
<input type="text" name="composer" parsley-trigger="change" required
value="<?php echo $data->composer; ?>" class="form-control" id="userName">
</div>
<button type="submit" class="btn btn-success waves-effect w-md waves-light m-b-5">Update</button>
<button type="button" class="btn btn-danger waves-effect w-md waves-light m-b-5">Reset</button>
</form>
<?php } ?>
And this is for Controller:
function index(){
$data['songs'] = $this->m_admin_data->Tampil_song_admin()->result();
$this->load->view('admin/v_admin_song',$data);
}
//Tambah Data
function add_song(){
$this->load->view('admin/v_admin_song_add');
}
function proses_tambah(){
$id_album = $this->input->post('id_album');
$song = $this->input->post('song');
$author = $this->input->post('author');
$composer = $this->input->post('composer');
$data = array(
'id_album' => $id_album,
'song' => $song,
'author' => $author,
'composer' => $composer
);
$this->m_admin_data->Input_song($data,'songs');
redirect('Admin_song/index');
}
//Update/Edit Data
function edit_song($id){
$where = array('id_song' => $id);
$data['songs'] = $this->m_admin_data->edit_song($where,'songs')->result();
$this->load->view('admin/v_admin_song_edit',$data);
}
function update_song(){
$id = $this->input->post('id_song');
$id_album = $this->input->post('id_album');
$song = $this->input->post('song');
$author = $this->input->post('author');
$composer = $this->input->post('composer');
$data = array(
'id_album' => $id_album,
'song' => $song,
'author' => $author,
'composer' => $composer
);
$where = array(
'id_song' => $id
);
$this->m_admin_data->update_song($where,$data,'songs');
redirect('Admin_song/index');
}
//Hapus Data
function delete_song($id){
$where = array('id_song' => $id);
$this->m_admin_data->delete_song($where,'songs');
redirect('Admin_song/index');
}
See I got nothing wrong but when I edit and try to update some data its not affected.
Hope this will help you :
You have disabled you id_song input type in your form just remove disabled attr from it
Because of this u r unable to get id_song post value in your update_song method in turn not getting in your where clause so
id_song input Should be like this :
<div class="form-group">
<label for="userName">id_songs </label>
<input type="text"
name="id_song"
value="<?php echo $data->id_song; ?>"
class="form-control" id="userName">
</div>
Or just make it hidden if you don't want to show it like this :
<input type="hidden"
name="id_song"
parsley-trigger="change" required
value="<?php echo $data->id_song; ?>"
class="form-control" id="userName">

codeigniter database error number 1064 on update data

I want to update user data in the Code-igniter (user details)
below is my code for more checking . please check it out why I get error when i push "update" button.
note: I used some of these information in my view that I didn't copied theme for now (no need in-fact )
controller:
public function edit(){
//validation form
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]|max_length[16]');
$this->form_validation->set_rules('password','Password','trim|min_length[3]|max_length[50]');
$this->form_validation->set_rules('password2','Confirm Password','trim|matches[password]');
$this->form_validation->set_rules('cellphone','Cellphone Number','trim|required');
$this->form_validation->set_rules('activation_code','Activation Code','trim');
//$data['groups'] = $this->User_model->get_groups();
//$data['user'] = $this->User_model->get_user($uid);
if($this->form_validation->run() == FALSE) {
//view
$data = array(
'userid' => $this->session->userdata('user_id')->id,
'first_name' => $this->session->userdata('user_id')->first_name,
'last_name' => $this->session->userdata('user_id')->last_name,
'fathers_name' => $this->session->userdata('user_id')->fathers_name,
'id_card_number' => $this->session->userdata('user_id')->id_card_number,
'national_number' => $this->session->userdata('user_id')->national_code,
'national_code' => $this->session->userdata('user_id')->national_code,
'age' => $this->session->userdata('user_id')->age,
'gender' => $this->session->userdata('user_id')->gender,
'is_addict' => $this->session->userdata('user_id')->is_addict,
'hiv_status' => $this->session->userdata('user_id')->hiv_status,
'hepatitis_status' => $this->session->userdata('user_id')->hepatitis_status,
'email' => $this->session->userdata('user_id')->email,
'needed_services' => $this->session->userdata('user_id')->needed_services,
'username' => $this->session->userdata('user_id')->username,
);
//$data['last_name'] = $this->session->userdata('user_id')->last_name;
//$data['data'] = $this->Profile_Model->get_user_profile();
//$this->base->set_message($msg,$type);
redirect(site_url('admin/profile'),'refresh');
$data['courses'] = $this->Profile_Model->get_user_profile();
//load view
$data['main_content'] = 'profile/index';
$this->load->view('profile/layouts/main', $data);
}else{
//create articles data array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'cellphone' => $this->input->post('cellphone'),
'group_id' => 2,
);
if($this->input->post('user_id') == $this->session->userdata('user_id')->id){
$uid = $this->input->post('user_id');
}
$password = $this->input->post('password');
$password2 = $this->input->post('password2');
if($password != '' && $password = $password2 ){
$data['password'] = md5($this->input->post('password'));
}
if($this->input->post('activation_code') !=''){
$data['activation_code'] = $this->input->post('activation_code');
}
$this->Profile_Model->update($data, $uid);
//Create Message
$this->session->set_flashdata('User_saved','You have successfully registered');
//redirect to page
redirect('admin/profile');
}
}
also below is my model snippet code:
model:
public function update($data, $uid)
{
$this->db->where('id', $uid);
$result = $this->db->update($this->table_users, $data);
return $result;
}
and below is my view form:
view:
<?php echo form_open(base_url('admin/profile/edit'),'class="form-horizontal"');?>
<?php $uid = $this->session->userdata('user_id')->id;
$uid = intval($uid);
?>
<div class="form-group">
<label class="col-sm-1 control-label" for="first_name">نام</label>
<input type="hidden" name="user_id" value="<?php echo $uid;?>">
<div class="col-sm-5">
<input type="text" id="first_name" name="first_name" class="form-control" value="<?php echo set_value('first_name',$first_name);?>">
</div>
<label class="col-sm-1 control-label" for="last_name">نام خانوادگی</label>
<div class="col-sm-5">
<input type="text" id="last_name" name="last_name" class="form-control" value="<?php echo set_value('last_name',$last_name);?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="email">ایمیل</label>
<div class="col-sm-5">
<input type="email" id="email" name="email" class="form-control" value="<?php /** #var dbObject $email */
echo set_value('email',$email);?>">
</div>
<label class="col-sm-1 control-label" for="username">نام کاربری</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $username;?>" disabled aria-disabled="true" title="You cant't change your username" />
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="password">رمز ورود</label>
<div class="col-sm-5">
<input type="password" id="password" name="password" class="form-control">
</div>
<label class="col-sm-1 control-label" for="password2">تایید رمز</label>
<div class="col-sm-5">
<input type="password" id="password2" name="password2" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="cellphone">شماره تلفن همراه</label>
<div class="col-sm-5">
<input type="tel" id="cellphone" name="cellphone" class="form-control">
</div>
<label class="col-sm-1 control-label" for="cellphone_activation">کد تایید پیامکی</label>
<div class="col-sm-5">
<input type="text" id="cellphone_activation" name="cellphone_activation" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-1">
<p>
<button class="btn mybtn btn-custom" type="submit">ویرایش</button>
</p>
</div>
</div>
<?php echo form_close();?>
Model Should be like this write table name instead of $this->table_users
public function update($data, $uid)
{
$this->db->where('id', $uid);
$result = $this->db->update(user, $data);
return $result;
}

I am new to Codeigniter can any one help me how to insert data into mysql? [duplicate]

This question already has answers here:
insert data into database with codeigniter
(9 answers)
Closed 6 years ago.
I am learning Codeigniter i want to insert data into database. How can i perform such task?
This is my Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class PostJobs extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Post_Model');
}
public function index(){
$this->load->helper(array('form', 'url', 'email'));
$this->load->view('postjobs');
}
public function post(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email',
array(
'required' => 'enter email id'));
$this->form_validation->set_rules('JTitle', 'Job Title', 'trim|required|callback__check_length[6]',
array(
'required' => 'enter JOB TITLE'));
$this->form_validation->set_rules('JType', 'Job Type', 'trim|required',
array(
'required' => 'select job type'));
$this->form_validation->set_rules('Salary', 'Salary', 'trim|required',
array(
'required' => 'enter salary'));
$this->form_validation->set_rules('Exp', 'Experience', 'trim|required',
array(
'required' => 'select Experience'));
$this->form_validation->set_rules('Skills', 'Skills', 'trim|required',
array(
'required' => 'enter skills'));
$this->form_validation->set_rules('EduReq', 'Education required', 'trim|required',
array(
'required' => 'enter EDUCATION'));
$this->form_validation->set_rules('JLoc', 'Location', 'trim|required',
array(
'required' => 'enter LOCATION'));
$this->form_validation->set_rules('CName', 'Company Name', 'trim|required',
array(
'required' => 'enter COMPANY NAME'));
$this->form_validation->set_rules('CAdd', 'Company Address', 'trim|required',
array(
'required' => 'enter COMPANY ADDRESS'));
$this->form_validation->set_rules('JDesc', 'Job Description', 'trim|required',
array(
'required' => 'enter JOB DESCRIPTION'));
if($this->form_validation->run() ==FALSE){
$this->load->view('postjobs');
}
else{
$data = array(
'email' => $this->input->post('email'),
'JTitle' => $this->input->post('JTitle'),
'JType' => $this->input->post('JType'),
'Salary' => $this->input->post('Salary'),
'Exp' => $this->input->post('Exp'),
'Skills' => $this->input->post('Skills'),
'EduReq' => $this->input->post('EduReq'),
'JLoc' => $this->input->post('JLoc'),
'CName' => $this->input->post('CName'),
'CWeb' => $this->input->post('CWeb'),
'CAdd' => $this->input->post('CAdd'),
'JDesc' => $this->input->post('JDesc')
);
$this->Post_Model->insert_postjob($data);
$this->load->view('postjobs');
}
}
function _check_length($input, $min){
$length = strlen($input);
if ($length >= $min){
return TRUE;
}
elseif ($length < $min){
$this->form_validation->set_message('_check_length', 'Minimum ' . $min. ' character required');
return FALSE;
}
}
}
This is my Model:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Post_Model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function insert_postjob($data){
$this->db->insert('jobs', $data);
}
}
jobs is table name
This my view
<div class="postjob">
<div class="container">
<h3>Post a Job</h3>
<form class="form-horizontal" role="form" id="postjobs" role="form" method="POST" action="<?=site_url('PostJobs/post');?>">
<div class="form-group">
<div class="col-lg-12">
<label for="jobemail">Email</label>
<span class="text-danger" style="display:inline-block; text-transform:uppercase;"><?php echo form_error('email');?></span>
<input type="email" id="emailjob" name="email" class="input-job form-control" value="<?php echo set_value('email'); ?>" autofocus>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="jobtitle">Job Title</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JTitle');?></span>
<input type="text" id="titlejob" name="JTitle" class="input-job form-control" value="<?php echo set_value('JTitle'); ?>">
</div>
<div class="col-lg-6">
<label for="jobtype">Job Type</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JType');?></span>
<select name="JType" class="input-job form-control" id="select" >
<option value="">SELECT</option>
<option value="Full Time">Full Time</option>
<option value="Part Time">Part Time</option>
<option value="Temporary">Temporary</option>
<option value="Internship">Internship</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="jobsalary">Salary</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Salary');?></span>
<input type="text" name="Salary" id="salaryjob" class="input-job form-control" value="<?php echo set_value('Salary');?>">
</div>
<div class="col-lg-6">
<label for="exp">Experience</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Exp');?></span>
<select name="Exp" class="input-job form-control" id="exp" >
<option value="">SELECT</option>
<option value="Fresher">Fresher</option>
<option value="< 1"> < 1</option>
<option value="1-2">1-2</option>
<option value="2-5">2-5</option>
<option value="5-8">5-8</option>
<option value="8-10">8-10</option>
<option value="10-12">10-12</option>
<option value="> 12">> 12</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="skills">Skills(Seperate by Comma)</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('Skills');?></span>
<input type="text" name="Skills" id="skill" class="input-job form-control" value="<?php echo set_value('Skills');?>">
</div>
<div class="col-lg-6">
<label for="education">Education Requirement</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('EduReq');?></span>
<input type="text" name="EduReq" id="EduReq" class="input-job form-control" value="<?php echo set_value('EduReq');?>">
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="joblocation">Job Location</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JLocation');?></span>
<input type="text" name="JLocation" id="locationjob" value="<?php echo set_value('JLocation');?>" class="input-job form-control">
</div>
<div class="col-lg-6">
<label for="cname">Company Name</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('CName');?></span>
<input type="text" name="CName" id="cname" value="<?php echo set_value('CName');?>" class="input-job form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="cweb">Company Website(Optional)</label>
<input type="url" name="CWeb" id="cweb" value="<?php echo set_value('CWeb');?>" class="input-job form-control">
</div>
<div class="col-lg-6">
<label for="cadd">Company Address</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('CAdd');?></span>
<input type="text" class="input-job form-control" name="CAdd" value="<?php echo set_value('CAdd');?>">
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<label for="jobdesc">Job Description</label>
<span class="text-danger" style="display:inline-block;text-transform:uppercase;"><?php echo form_error('JDesc');?></span>
<textarea class="input-job form-control textarea" name="JDesc" rows="10" id="jdesc" placeholder="Description" style="resize:none;"><?php echo set_value('JDesc');?></textarea>
</div>
</div>
<div class="form-group text-center">
<input type="submit" name="btnpost" class="btn btn-green" value="POST JOBS"/>
</div>
</form>
</div>
</div>
Can anyone help me where i am failing to insert data??
Try to make your model without public:
$query = $this->db->insert('jobs', $data);
if ($query) {
return true;
} else {
return false;
}
Check if the table column names match to what you are trying to insert to, they might be wrong...
Use $this->db->last_query(); to see your generate query, and try to insert it manually and run it, if it fails you will see why :)
In your controller replace :
$this->Post_Model->insert_postjob($data);
with
$this->post_model->insert_postjob($data); // lowercase
In your model add following to your construct function
$this->load->database(); // if not added in autoload

Unable to retrieve ID to update in database

I am unable to identify the mistake
model (here I am matching the id to update)
function get_account_record($a_id)
{
$this->db->where('a_id', $a_id);
$this->db->from('account_info');
$query = $this->db->get();
return $query->result();
}
controller (receiving everything via post accept the ID i-e a_id )
function update($a_id)
{
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
form Validation
if ($this->form_validation->run() == FALSE)
{
$this->load->view('viewUpdate', $data);
}
else {
$data = array(
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_mobile' => $this->input->post('a_mobile'),);
$this->db->where('a_id', $a_id);
$this->db->update('account_info', $data);
display success message
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Successfully Updated!</div>');
//redirect('salesforce' . $a_id);
}
}
view (ID and Name)
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_id" class="control-label">Account ID</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" disabled="disabled" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_name" class="control-label">Account Name</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control" value="<?php echo $view[0]->a_name; ?>" />
<span class="text-danger"><?php echo form_error('a_name'); ?></span>
</div>
</div>
</div>
If you don't want to allow the user to edit then add readonly instead of disabled attribute
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" readonly="readonly" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
If you want to disable then add the hidden element
<input id="a1_id" name="a1_id" type="hidden" value="<?php echo $a_id;?>" />
Controller Function
function update($a_id)
{
//read the value using input library
$a_id = $this->input->post('a_id');
//$a1_id = $this->input->post('a1_id'); your hidden element value can be get here.
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
}
My problem Solved after editing the where clause in update query
$this->db->where('a_id', $_POST['a_id']);

Codeigniter: Parser issue in a form. Only one is saved

I have a strange issue with one form in Codeigniter. I am also using DataMapper PHP. So I have one form with two select statements with a couple of options. If one is selected the other one becomes 0 if the other is selected then the first one becomes 0. Any ideas why the strange behavior ?
Controller
public function edit($id)
{
// Check if the user is logged in
if (!$this->ion_auth->logged_in())
{
redirect('/');
}
elseif ($this->ion_auth->is_admin())
{
// Create Object
$predictions = new Prediction_model();
$predictions->where('id', $id)->get();
$categories = new Categories_model();
$bookmakers = new Bookmakers_model();
$categories->order_by('id', 'asc')->get();
$bookmakers->order_by('id', 'asc')->get();
// Default Object Options
$defaultCategory = new Categories_model();
$defaultCategory->where('id', $predictions->category_id)->get();
$defaultBookmaker = new Bookmakers_model();
$defaultBookmaker->where('id', $predictions->bookmaker_id)->get();
$recent_categories = array();
foreach ($categories as $category)
{
$single_category = array
(
'category_id' => $category->id,
'sport' => $category->sport,
);
array_push($recent_categories, $single_category);
}
$recent_bookmakers = array();
foreach ($bookmakers as $bookmaker)
{
$single_bookmaker = array
(
'bookmaker_id' => $bookmaker->id,
'bookmaker' => $bookmaker->bookmaker,
);
array_push($recent_bookmakers, $single_bookmaker);
}
// Validation rules
$rules = $this->prediction_model->rules;
$this->form_validation->set_rules($rules);
$data = array
(
'prediction_id' => $predictions->id,
'defaultCategory' => $defaultCategory->sport,
'defaultBookmaker' => $defaultBookmaker->bookmaker,
'eventDate' => $predictions->eventDate,
'event' => $predictions->event,
'tip' => $predictions->tip,
'bestOdd' => $predictions->bestOdd,
'bookmakers' => $recent_bookmakers,
'categories' => $recent_categories,
'admin_content' => 'admin/predictions/edit',
);
if ($this->input->post('submit'))
{
$predictions->eventDate = $this->input->post('eventDate');
$predictions->event = $this->input->post('event');
$predictions->tip = $this->input->post('tip');
$predictions->bestOdd = $this->input->post('bestOdd');
$predictions->bookmaker_id = $this->input->post('bookmaker');
$predictions->category_id = $this->input->post('icon');
}
if ($this->form_validation->run() == FALSE)
{
$this->parser->parse('admin/template_admin', $data);
}
else
{
$predictions->save();
redirect('admin/predictions/');
}
}
else
{
// NO ACCESS
}
} // function edit
and here is the VIEW:
<?php echo form_open('admin/predictions/edit/{prediction_id}', 'class="form-horizontal"'); ?>
<div class="form-group">
<label for="category" class="col-sm-2 control-label">Category</label>
<div class="col-sm-10">
<select name="icon" class="form-control">
<option selected='selected'>{defaultCategory}</option>
<option>--------</option>
{categories}
<option value="{category_id}">{sport}</option>
{/categories}
</select>
</div>
</div>
<div class="form-group">
<label for="event" class="col-sm-2 control-label">Event</label>
<div class="col-sm-10">
<input type="text" name="event" class="form-control" value="{event}">
</div>
</div>
<div class="form-group">
<label for="tip" class="col-sm-2 control-label">Tip</label>
<div class="col-sm-10">
<input type="text" name="tip" class="form-control" value="{tip}">
</div>
</div>
<div class="form-group">
<label for="bookmaker" class="col-sm-2 control-label">Bookmaker</label>
<div class="col-sm-5">
<select name="bookmaker" class="form-control">
<option selected='selected'>{defaultBookmaker}</option>
<option>--------</option>
{bookmakers}
<option value="{bookmaker_id}">{bookmaker}</option>
{/bookmakers}
</select>
</div>
<label for="bestOdd" class="col-sm-2 control-label">Odd</label>
<div class="col-sm-3">
<input type="text" name="bestOdd" class="form-control" value="{bestOdd}">
</div>
</div>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">Date</label>
<div class="col-sm-4">
<div class="input-group">
<input size="16" name="eventDate" type="text" class="form-control eventDate" value="{eventDate}" readonly>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" name="submit" value="Save" class="btn btn-success btn-lg">
</div>
</div>
<?php echo form_close(); ?>

Categories