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

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

Related

ArgumentCountError - Too few arguments

I am just learning codeigniter and I have a page to update my database. but I get an error like this:
Type: ArgumentCountError
Message: Too few arguments to function Inhouse::ubah(), 0 passed in D:\xampp\htdocs\slc\system\core\CodeIgniter.php on line 532 and exactly 1 expected
Filename: D:\xampp\htdocs\slc\application\controllers\Inhouse.php
Line Number: 120
How can I fix this problem? This is my code:
Controller :
public function ubah($id)
{
$data['title'] = 'Change Form - Data IHT Program';
$data['inhouse'] = $this->Inhouse_model->getInhouseById($id);
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('subtitle', 'Subtitle', 'required');
$this->form_validation->set_rules('overview', 'Overview', 'required');
$this->form_validation->set_rules('goals', 'Goals', 'required');
$this->form_validation->set_rules('agenda1', 'Agenda 01', 'required');
$this->form_validation->set_rules('agenda2', 'Agenda 02', 'required');
$this->form_validation->set_rules('agenda3', 'Agenda 03', 'required');
$this->form_validation->set_rules('agenda4', 'Agenda 04', 'required');
$this->form_validation->set_rules('agenda5', 'Agenda 05', 'required');
$this->form_validation->set_rules('agenda6', 'Agenda 06', 'required');
$this->form_validation->set_rules('agenda7', 'Agenda 07', 'required');
$this->form_validation->set_rules('agenda8', 'Agenda 08', 'required');
$this->form_validation->set_rules('trainer', 'Trainer', 'required');
if ($this->form_validation->run() == FALSE) {
if (!$this->session->userdata('email')) {
$this->load->view('templates/header', $data);
} else {
$this->load->view('templates/login_header', $data);
}
$this->load->view('inhouse/ubah', $data);
$this->load->view('templates/footer');
} else {
// cek jika ada gampar yg diupload
$upload_image = $_FILES['image']['name'];
if ($upload_image) {
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5048';
$config['upload_path'] = './assets/img/inhouse/';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$old_image = $data['inhouse']['image'];
if ($old_image != 'default.jpg') {
unlink(FCPATH . 'assets/img/inhouse/' . $old_image);
} else {
$new_image = $this->upload->data('file_name');
$this->db->set('image', $new_image);
}
} else {
echo $this->upload->display_errors();
}
}
$this->db->set('image', $new_image);
$this->db->where('id', $id);
$this->db->update('inhouse', $data);
$this->Inhouse_model->ubahInhouseProgram();
$this->session->set_flashdata('flash', 'Diubah!');
redirect('admin/product');
}
}
Model :
public function ubahInhouseProgram()
{
$data = [
"image" => $this->input->post('image', true),
"title" => $this->input->post('title', true),
"subtitle" => $this->input->post('subtitle', true),
"overview" => $this->input->post('overview', true),
"goals" => $this->input->post('goals', true),
"agenda1" => $this->input->post('agenda1', true),
"agenda2" => $this->input->post('agenda2', true),
"agenda3" => $this->input->post('agenda3', true),
"agenda4" => $this->input->post('agenda4', true),
"agenda5" => $this->input->post('agenda5', true),
"agenda6" => $this->input->post('agenda6', true),
"agenda7" => $this->input->post('agenda7', true),
"agenda8" => $this->input->post('agenda8', true),
"trainer" => $this->input->post('trainer', true)
];
$this->db->where('id', $this->input->post('id'));
$this->db->update('inhouse', $data);
}
View :
<div class="container">
<div class="row mt-5 mb-5">
<div class="col-lg-12">
<!-- FORM -->
<div class="card myshadow">
<div class="card-header font-weight-bold">
<h2>Change Form Data IHT Program</h2>
</div>
<div class="card-body">
<?= form_open_multipart('inhouse/ubah'); ?>
<input type="hidden" name="id" value="<?= $inhouse['id']; ?>">
<div class="form-group mt-4">
<div class="">Picture</div>
<div class="row">
<div class="col-sm-4">
<img src="<?= base_url('assets/img/inhouse/') . $inhouse['image']; ?>" class="img-thumbnail">
</div>
<div class="col-sm-8">
<div class="custom-file">
<input type="file" class="custom-file-input" id="image" name="image">
<label class="custom-file-label" for="image">Choose file</label>
</div>
</div>
</div>
</div>
<div class="form-group mt-4">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="<?= $inhouse['title'] ?>">
<small class="form-text text-danger"><?= form_error('title') ?></small>
</div>
<div class="form-group mt-4">
<label for="subtitle">Subtitle</label>
<textarea type="text" name="subtitle" class="form-control" id="subtitle" rows="3"><?= $inhouse['subtitle']; ?></textarea>
<small class="form-text text-danger"><?= form_error('subtitle') ?></small>
</div>
<div class="form-group mt-4">
<label for="overview">Overview</label>
<textarea type="text" name="overview" class="form-control" id="overview" rows="8"><?= $inhouse['overview'] ?></textarea>
<small class="form-text text-danger"><?= form_error('overview') ?></small>
</div>
<div class="form-group mt-4">
<label for="goals">Goals</label>
<textarea type="text" name="goals" class="form-control" id="goals" rows="3"><?= $inhouse['goals'] ?></textarea>
<small class="form-text text-danger"><?= form_error('goals') ?></small>
</div>
<hr class="mt-5">
<div class="form-group mt-4">
<label for="agenda1">Agenda 01</label>
<input type="text" name="agenda1" class="form-control" id="agenda1" value="<?= $inhouse['agenda1'] ?>">
<small class="form-text text-danger"><?= form_error('agenda1') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda2">Agenda 02</label>
<input type="text" name="agenda2" class="form-control" id="agenda2" value="<?= $inhouse['agenda2'] ?>">
<small class="form-text text-danger"><?= form_error('agenda2') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda3">Agenda 03</label>
<input type="text" name="agenda3" class="form-control" id="agenda3" value="<?= $inhouse['agenda3'] ?>">
<small class="form-text text-danger"><?= form_error('agenda3') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda4">Agenda 04</label>
<input type="text" name="agenda4" class="form-control" id="agenda4" value="<?= $inhouse['agenda4'] ?>">
<small class="form-text text-danger"><?= form_error('agenda4') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda5">Agenda 05</label>
<input type="text" name="agenda5" class="form-control" id="agenda5" value="<?= $inhouse['agenda5'] ?>">
<small class="form-text text-danger"><?= form_error('agenda5') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda6">Agenda 06</label>
<input type="text" name="agenda6" class="form-control" id="agenda6" value="<?= $inhouse['agenda6'] ?>">
<small class="form-text text-danger"><?= form_error('agenda6') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda7">Agenda 07</label>
<input type="text" name="agenda7" class="form-control" id="agenda7" value="<?= $inhouse['agenda7'] ?>">
<small class="form-text text-danger"><?= form_error('agenda7') ?></small>
</div>
<div class="form-group mt-4">
<label for="agenda8">Agenda 08</label>
<input type="text" name="agenda8" class="form-control" id="agenda8" value="<?= $inhouse['agenda8'] ?>">
<small class="form-text text-danger"><?= form_error('agenda8') ?></small>
</div>
<div class="form-group mt-4">
<label for="trainer">Trainer</label>
<input type="text" name="trainer" class="form-control" id="trainer" value="<?= $inhouse['trainer'] ?>">
<small class="form-text text-danger"><?= form_error('trainer') ?></small>
</div>
<button type="submit" name="ubah" class="btn btn-primary mt-5">Change Data</button>
</form>
</div>
</div>
<!-- END FORM -->
</div>
</div>
</div>
Get Argument like this "$this->uri->segment(2)"
exp:- htts://localhost/ci/50/60
$this->uri->segment(2) // O/P 50
$this->uri->segment(3) // O/P 60
for more information go here Segments
public function ubah(){
$this->uri->segment(2);
}
I guess , this error occurs only when you try to access to controler without arguments like
http://localhost/slc/index.php/ubah
If you access the controler like following , there shouldn't be an error
http://localhost/slc/index.php/ubah/2
You can fix this by setting a default value to $id
public function ubah($id = -1){
if($id != -1){
//put your code here
}else{
show_404();
}
}
Thanks for all of your answers, i really appreciate you guys for helping me to fix my code..
but, i just removing the "action" from my form_opener_multipart();
From this :
<?= form_open_multipart('inhouse/ubah'); ?>
Into this :
<?= form_open_multipart(); ?>
I do not really understand why it can be like that, but now my program can be run, can you guys explain to me? i'll really appreciate that.
And there is another problem, the other column of my database table can be updated except "the image" column.

Load a specific div on id base codeigniter

myiamge
I have 2 forms as tab in signup page when one form is submit and if have any error I want to redirect to that specific tab which have error but did not happen.
Here is my view
<?php include 'header.php'; ?>
<section class="services">
<div class="container">
<div class="signup-wrapper well" style="margin-bottom: 80px; margin-top: 80px;">
<div class="container">
<div class="services__main">
<h2 class="title title--main"><span class="title__bold">Sign Up</span><span class="line line--title"><span class="line__first"></span><span class="line__second"></span></span></h2>
</div>
<div class="aside-tabs__links">
Genrel User
Dealership
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc" >
<div class="col-md-5">
<form action="<?= base_url();?>Home/add_user" method="post" >
<!-- class="quick-form" -->
<div class="form-group">
<input type="text" class="form-control" name="fname" placeholder="Full Name" />
</div>
<?php echo form_error('fname'); ?>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" />
</div>
<?php echo form_error('email'); ?>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" />
</div>
<?php echo form_error('password'); ?>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone #" />
</div>
<?php echo form_error('phone'); ?>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<?php echo form_error('city'); ?>
<div class="form-group">
<input type="submit" class="btn button button--red button--main pull-right" style="margin-bottom: 10px;" value="Sign Up">
</div>
</form>
</div>
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev" style="display: none;">
<div class="col-xs-5">
<form action="<?= base_url();?>Home/add_dealer" method="post" enctype="multipart/form-data">
<h4>Comapny Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="cname" placeholder="Company Name" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="owner" placeholder="Owner Name" />
</div>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Office Location" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Office Phone" />
</div>
<div class="form-group">
<select name="business-type" class="form-control">
<option class="form-group">Products Deal</option>
<option class="form-group">Bikes</option>
<option class="form-group">Accessories</option>
<option class="form-group">Both</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="link" placeholder="social-link(optional)" />
</div>
<div class="form-group">
<button type="button" class="button button--custom--grey button--main btn" id="logo">Upload Logo</button>
<input type="file" class="form-control hidden" id="logo1" name="logo1" /><span id="mylogo">* Format must be jpg,jpeg or png</span>
</div>
<?php echo form_error('logo'); ?>
<div class="form-group">
<textarea class="form-control" name="descrp" placeholder="Description(optional)"></textarea>
</div>
<h4>Primary Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="password" placeholder="Password" />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn button button--red button--main pull-right" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
here is my controller
function add_dealer()
{
//echo "<pre>"; print_r($_FILES);
$config['upload_path'] = 'C:\xampp\htdocs\devilbirds\images\uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = 'TRUE';
$config['overwrite'] = 'FALSE';
$config['wm_text'] = 'Deveil Birds';
$config['wm_type'] = 'text';
$config['wm_font_path'] = 'C:\xampp\htdocs\devilbirds/fonts/fontawesome-webfont.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'aabbcc';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->load->library('upload', $config);
$this->image_lib->watermark();
$abc = $this->upload->do_upload('logo1');
$upload_data = $this->upload->data();
// var_dump($abc);exit();
$file_name = $upload_data['file_name'];
$this->form_validation->set_rules('cname','CName','trim|required');
$this->form_validation->set_rules('logo1','Logo1','trim');
$this->form_validation->set_rules('address','Address','trim');
$this->form_validation->set_rules('phone','Phone','trim|is_unique[bd_dealer.phone]');
$this->form_validation->set_rules('owner','Owner','trim');
$this->form_validation->set_rules('descrp','Descrp','trim');
$this->form_validation->set_rules('business-type','Business-type','trim');
$this->form_validation->set_rules('link','Link','trim');
$this->form_validation->set_rules('email','Email','trim|is_unique[bd_dealer.email]');
$this->form_validation->set_rules('password','Password','trim|min_length[5]|max_length[20]');
$this->form_validation->set_rules('city','City','trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = $this->session->set_flashdata('errors');
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url
}
else
{
$userData = array(
'company' => $this->input->post('cname'),
'logo' => $file_name,
'location' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'name' => $this->input->post('owner'),
'description' => $this->input->post('descrp'),
'business_type' => $this->input->post('business-type'),
'social_link' => $this->input->post('link'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'city' => $this->input->post('city'));
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'user_type' => '2');
//var_dump($userData);exit();
$this->Home_m->add_dealer($userData);
$this->Home_m->users($data);
redirect('Home/login');
}
}
I have two functions for two different forms in tabs. let suppose if i post my dealer form and this form have any issue then it will redirect to the signup page and it shows the pre-active tab which is general user signup. i want to redirect to the div that contain the form or dealer signup. any help will be appreciated , thanks in advance
In your controller when you get error do this.
for form one error
$data['error'] = $this->session->set_flashdata('errors');
$data['div1'] = "div1";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
For form two error
$data['error'] = $this->session->set_flashdata('errors');
$data['div2'] = "div2";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
In your view page
<script>
$(document).ready(function(){
var div1 = "<?php $div1; ?>";
var div2 = "<?php $div2; ?>";
if(div1=='' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='div1' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='' && div2=='div2'){
$("#desc").hide();
$("#rev").show();
}
})
</script>
div1
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc">
Div2
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev">
I hope it will help you.

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

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'

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']);

Categories