codeigniter database error number 1064 on update data - php

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

Related

Unable to update profile data into database after user login using codeigniter

Once user login into the website it will fetch the details of the login users based on profile ids.If i try to update the details of the user unable to update as well as not getting any issues.
Controller:
function index()
{
if($this->session->userdata('admin_logged_in'))
{
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['country'] = $this->signup_model->getcountry();
$data['states'] = $this->profile_model->getstates();
$data['records']= $this->profile_model->getprofiledata($this->session->userdata('admin_logged_in')['profile_id']);
$data['mainpage']='profile';
//echo '<pre>'.print_r($data, true).'</pre>';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else
{
$this->load->view('welcome');
}
}
function updateprofile()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('first_name','First Name','required');
$this->form_validation->set_rules('profile_name','Profile Name','required');
$this->form_validation->set_rules('designation','Designation','required');
$this->form_validation->set_rules('address_1','Address','required');
$this->form_validation->set_rules('address_2','Address','required');
if($this->form_validation->run()== FALSE)
{
$data['records']= $this->profile_model->getprofiledata($this->session->userdata('admin_logged_in')['profile_id']);
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else
{
$result = $this->profile_model->update($this->input->post('profile_id'));
if(is_array($result))
{
$data['errors']=$result;
$data['records']= $this->profile_model->getprofiledata($this->session->userdata('admin_logged_in')['profile_id']);
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else
$this->flash->success('<h2>Successfully Updated the record.<h2>');
redirect('profile');
}
}
Model:
function getprofiledata($id)
{
$this->db->select('profile_details.*,C.country_name,S.state_name,D.city_name');
$this->db->from('profile_details');
$this->db->join('countries AS C','C.country_id=profile_details.country_id','INNER');
$this->db->join('states AS S','S.state_id=profile_details.state_id','INNER');
$this->db->join('cities AS D','D.city_id=profile_details.city_id','INNER');
$this->db->where(array('profile_details.profile_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function update($id)
{
$data=array(
'first_name' =>$this->input->post('first_name'),
'profile_name' =>$this->input->post('profile_name'),
'designation' =>$this->input->post('designation'),
'address_1' =>$this->input->post('address_1'),
'address_2' =>$this->input->post('address_2')
);
$this->db->where(array('profile_id'=>$id));
$this->db->update('profile_details', $data);
return true;
}
View:
<div class="col-md-3">
</div>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<form action="<?php echo base_url();?>profile/updateprofile" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<?php
echo form_hidden('profile_id',$r->profile_id);
?>
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" nae="first_name" id="first_name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="profilename">Profile Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="profile_name" id="profile_name" placeholder="Enter Profile name" value="<?php echo $r->profile_name;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="designation">Designation:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="designation" id="designation" placeholder="Enter Designation" value="<?php echo $r->designation;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="address_1">Address 1:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="address_1" name="address_1" placeholder="Enter Address Details" value="<?php echo $r->address_1;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="address_2">Address 2:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="address_2" name="address_2" placeholder="Enter Address Details" value="<?php echo $r->address_2;?>" />
</div>
</div>
<button type="submit" class="btn">Submit</button>
</form>
<?php endforeach;endif;?>
</div>
Once user login into the website unable to update the data into database.Once submitting the form redirecting to same page not getting any issue as well.
Dear user7047368,
Maybe problems at your model code.
function update($id)
{
$data=array(
'first_name' =>$this->input->post('first_name'),
'profile_name' =>$this->input->post('profile_name'),
'designation' =>$this->input->post('designation'),
'address_1' =>$this->input->post('address_1'),
'address_2' =>$this->input->post('address_2'),
);
$this->db->where(array('profile_id'=>$id));
$this->db->update('profile_details', $data);
return true;
}
MUST BE (you type an extra comma in $data(array))
function update($id)
{
$data=array(
'first_name' =>$this->input->post('first_name'),
'profile_name' =>$this->input->post('profile_name'),
'designation' =>$this->input->post('designation'),
'address_1' =>$this->input->post('address_1'),
'address_2' =>$this->input->post('address_2')
);
$this->db->where(array('profile_id'=>$id));
$this->db->update('profile_details', $data);
return true;
}
If you not sure your query is true or false, you can put this line var_dump($this->db->last_query()); into this function after $this->db->update('profile_details', $data);; then copy it and paste into Query Form in your phpmyadmin.
Hope this help.
Problem is in your view
Use this
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="first_name" id="first_name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
instead of
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" nae="first_name" id="first_name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
In your first name input field 'name' attribute has spell mistake.
Correct that 'nae' to 'name'

input value returns when form validation runs false

View
<div class="box-body">
<div class="form-group">
<label for="FNAME" class="col-sm-2 control-label col-sm-offset-2"> <span>*</span>First Name:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="FNAME" name="FNAME">
</div>
</div>
<div class="form-group has-error col-sm-offset-7">
<label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label>
</div>
</div>
Controller
public function create_id() {
$this->form_validation->set_rules('FNAME', 'First Name' ,'trim|required|max_length[30]');
$this->form_validation->set_rules('MNAME', 'Middle Name' ,'trim|max_length[30]');
$this->form_validation->set_rules('SURNAME', 'Last Name' ,'trim|required|max_length[30]');
if($this->form_validation->run($this) == FALSE) {
$this->add_view();
} else {
if($query = $this->Employees_Model->insert()) {
$this->autoid();
redirect('Employees/index');
} else {
$this->add_view();
}
}
}
What I want is if($this->form_validation->run($this) == FALSE){as you see it will redirect back to view. $this->add_view(); all I want is when it redirects back, the data that I input will remain. so that I will not input it again when the validation fails.
As per CodeIgniter documentation, you need to alter your view file a little bit, in the means of, printing those input element values with <?= set_value("FNAME"); ?> in the input element _value_ parameter. So your
<input type="text" class="form-control" id="FNAME" name="FNAME">
would become
<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= set_value("FNAME"); ?>">
and so on. This way you will tell CodeIgniter to re-populate the form after an error.
You can use set_value function.
view file
<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?php echo set_value('FNAME'); ?>">
Get the posted value like this
public function create_id() {
$this->form_validation->set_rules('FNAME', 'First Name' ,'trim|required|max_length[30]');
$this->form_validation->set_rules('MNAME', 'Middle Name' ,'trim|max_length[30]');
$this->form_validation->set_rules('SURNAME', 'Last Name' ,'trim|required|max_length[30]');
$data = array(
'FNAME' => $this->input->post('FNAME'),
'MNAME' => $this->input->post('MNAME'),
'SURNAME' => $this->input->post('SURNAME')
);
if($this->form_validation->run($this) == FALSE) {
$this->add_view($data);
} else {
if($query = $this->Employees_Model->insert()) {
$this->autoid();
redirect('Employees/index');
} else {
$this->add_view();
}
}
}
In your function
function add_view($data){
.............................
.............................
$this->data['values'] = $data;
.............................
.............................
}
Then
<div class="box-body">
<div class="form-group">
<label for="FNAME" class="col-sm-2 control-label col-sm-offset-2"><span>*</span>First Name:</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= $values['FNAME'] ?>">
</div>
</div>
<div class="form-group has-error col-sm-offset-7">
<label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label>
</div>
</div>

I can get all $_POST data except for one

I'm trying to input a record into my database. I can get all data except for one ['inputPicture'](which is the picture file name)
I've already tried using var_dum($_POST) and print_r($_POST) so I am sure that I cannot get anything from 'inputPicture'
This is what I get from both of them:
Array ( [inputFirstName] => Charlie [inputLastName] => Horse [inputContactNumber] => 09154447896 [inputAddress] => Candy Mountain [inputEmailAddress] => charlie#candymountain.com [action] => )
Here is my view code:
<form class="form-horizontal" role="form" method="POST" action="<?php echo base_url('Contacts/addcontact'); ?>" enctype="multipart/form-data">
<div class="form-group">
<label for="usr">First Name</label>
<input type="text" class="form-control" name="inputFirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="usr">Last Name</label>
<input type="text" class="form-control" name="inputLastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="usr">Contact Number</label>
<input type="text" class="form-control" name="inputContactNumber" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="usr">Address:</label>
<input type="text" class="form-control" name="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="usr">Email Address:</label>
<input type="text" class="form-control" name="inputEmailAddress" placeholder="Email Address">
</div>
<div class="form-group">
<label for="usr">Picture:</label>
<input type="file" class="form-control" name="inputPicture"></br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="action">Add</button>
</div>
</form>
Here is my controller code:
public function addcontact(){
$first_name = $this->input->post('inputFirstName');
$last_name = $this->input->post('inputLastName');
$contact_number = $this->input->post('inputContactNumber');
$address = $this->input->post('inputAddress');
$email_address = $this->input->post('inputEmailAddress');
$image_url = $this->input->post('inputImage');
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == FALSE){
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
else{
if(!isset($_POST['inputImage'])){
$this->contacts_model->addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $image_url;
$this->contacts_model->addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
}
I have a similar method which is 'update'. It works perfectly fine. I tried copy pasting my code from the update to my addContact method but it still doesnt work.
Here is my view code for Update:
<form class="form-horizontal" role="form" method="POST" action="<?php echo base_url('Contacts/update'); ?>" enctype="multipart/form-data">
<div class="form-group hidden">
<label for="usr">ID</label>
<input type="text" class="form-control" name="inputID" id="id">
</div>
<div class="form-group">
<label for="usr">First Name</label>
<input type="text" class="form-control" name="inputFirstName" id="firstname">
</div>
<div class="form-group">
<label for="usr">Last Name</label>
<input type="text" class="form-control" name="inputLastName" id="lastname">
</div>
<div class="form-group">
<label for="usr">Contact Number</label>
<input type="text" class="form-control" name="inputContactNumber" id="contactnumber">
</div>
<div class="form-group">
<label for="usr">Address:</label>
<input type="text" class="form-control" name="inputAddress" id="address">
</div>
<div class="form-group">
<label for="usr">Email Address:</label>
<input type="text" class="form-control" name="inputEmailAddress" id="emailaddress">
</div>
<div class="form-group">
<label for="usr">Picture:</label>
<input type="file" class="form-control" name="inputPicture" id="picture"></br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="action">Update</button>
</div>
</form>
and here is my controller
public function update(){
$first_name = $this->input->post('inputFirstName');
$last_name = $this->input->post('inputLastName');
$contact_number = $this->input->post('inputContactNumber');
$address = $this->input->post('inputAddress');
$email_address = $this->input->post('inputEmailAddress');
$image_url = $this->input->post('inputPicture');
$id = $this->input->post('inputID');
var_dump($_POST);exit;
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == FALSE){
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
else{
if(!isset($_POST['inputPicture'])){
$this->contacts_model->updateContactNoPic($id, $first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $image_url;
$this->contacts_model->updateContact($id, $first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
}
That's because contents of input type=file sent by GET/POST methods from HTML to PHP are stored inside the superglobal variable $_FILES and not $_POST (unless you don't define enctype property of the form tag as "multipart/form-data", which then causes the filename to be passed as a string to GET/POST).
If you var_dump($_FILES)/print_r($_FILES) you'll see an array like this:
Array
(
[file] => Array
(
[name] => test.pdf
[type] => application/pdf
[tmp_name] => C:\Windows\Temp\php1485.tmp
[error] => 0
[size] => 1073054
)
)
OBS: be sure to have enctype="multipart/form-data" as property of your form and file_uploads set to on in your php.ini file.
use $_FILES['inputPicture']
public function update(){
$first_name = $this->input->post('inputFirstName');
$last_name = $this->input->post('inputLastName');
$contact_number = $this->input->post('inputContactNumber');
$address = $this->input->post('inputAddress');
$email_address = $this->input->post('inputEmailAddress');
//$image_url = $this->input->post('inputPicture');
$image_url = $_FILES['inputPicture'];
$id = $this->input->post('inputID');
var_dump($_POST);exit;
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == FALSE){
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
else{
if(!isset($_FILES['inputPicture'])){
$this->contacts_model->updateContactNoPic($id, $first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $image_url;
$this->contacts_model->updateContact($id, $first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
}

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