Error updating unique data in CodeIgniter - php

I'm using CodeIgniter. I can't update data, because I make the database field is unique.
My controller:
function simpanadditional(){
$this->form_validation->set_rules('nama','Nama Additional','trim|required');
$this->form_validation->set_rules('harga','Harga Additional','required');
if($this->form_validation->run() == FALSE){
redirect('tambah-additional','refresh');
}else{
$cek = $this->my_model->ceknamadditional($this->input->post('nama'));
if(count($cek) > 0){
$this->session->set_flashdata('message','nama tidak boleh sama');
redirect('tambah-additional','refresh');
}else{
$this->my_model->simpanadditional();
redirect('admin/tabel_additional','refresh');
}
}
}
function editadditional($id){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['title'] = 'Edit Additional';
$data['main'] = 'admin/additional/form_edit_additional';
$data ['getdata'] = $this->my_model->editadditional($id);
$data['getgroup'] = $this->my_model->editgroup($session_data['username']);
$this->load->view('admin/index',$data);
} else {
redirect('admin/login', 'refresh');
}
}
My model:
function simpanadditional(){
$data = array(
'nama_additional' => $this->input->post('nama'),
'harga_additional' => $this->input->post('harga'),
);
if($this->input->post('id')){
// ini untuk update nya
$this->db->where('id_additional',$id);
$this->db->update('additional',$data);
}else{
// ini untuk insert nya
$this->db->insert('additional',$data);
}
}
function editadditional($id){
$data = array();
$this->db->select('*');
$this->db->from('additional');
$this->db->where('id_additional',$id);
$hasil = $this->db->get();
if($hasil->num_rows() > 0){
$data = $hasil->result();
}
$hasil->free_result();
return $data;
}
This is views of form additional:
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Nama <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2" name="nama" value="<?php echo $data->nama_additional ?>" type="text">
<input type="hidden" name="id" value="<?php echo $data->id_additional ?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Harga <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="icon" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" data-validate-words="2"
name="harga" value="<?php echo $data->harga_additional ?>" type="text" onkeypress="return isNumberKey(event)">
</div>
</div>
Can anyone help?

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'

Unable to access an error message - Callback validation in CodeIgniter

I have simple code thats not working. I made validation for checkbox and I get error:
Unable to access an error message corresponding to your field name accept_terms_checkbox.(accept_terms)
This is my code:
Controller:
public function formularz2()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['szkolenia'] = $this->Szkolenie_m->pobierz();
//VALIDATION!!
$this->form_validation->set_rules('imie', 'Imię', 'required');
$this->form_validation->set_message('required', 'Błąd: wypełnij powyższe pole');
$this->form_validation->set_rules('accept_terms_checkbox', 'checkbox', 'callback_accept_terms');
//CALLBACK FUNCTION!!
function accept_terms()
{
if ($this->input->post('accept_terms_checkbox'))
{
return TRUE;
}
else
{
$error = 'Please read and accept our terms and conditions.';
$this->form_validation->set_message('accept_terms', $error);
return FALSE;
}
}
//Po przesłaniu danych
if (!empty($_POST))
{
$konsultant = $this->uri->segment(3);
$dane = array(
'email1' => $this->input->post('email'),
'imie' => $this->input->post('imie'),
'nazwisko' => $this->input->post('nazwisko'),
'nazwa_firmy' => $this->input->post('firma'),
'konsultant_id' => $konsultant,
);
//Trzeba jeszcze tutaj ogarnąć wrzucanie pivotów zaznaczonych checkboxów
$boxes = $_POST['formChecks'];
$N = count($boxes);
$ostatni_id = $this->Osoby_m->ostatni_id();
for($i=0; $i < $N; $i++)
{
$this->Osoby_m->nowy_wpis_formularz($boxes[$i]);
}
//Pobrać id dodanej osoby = policzyć ile osób w bazie +1
$this->Osoby_m->nowa_osoba($dane);
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formularz', $data);
}
else
{
$this->load->view('wyslano_formularz');
}
}
My View:
<?php echo form_open(); ?>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>">
<p class="help-block"><?php echo form_error('email'); ?></p>
</div>
</div>
<div class="form-group">
<label for="imie" class="col-sm-2 control-label">Imię:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="imie" name="imie" placeholder="Imię" value="<?php echo set_value('imie'); ?>">
<p class="help-block"><?php echo form_error('imie'); ?></p>
</div>
</div>
<div class="form-group">
<label for="nazwisko" class="col-sm-2 control-label">Nazwisko: </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nazwisko" name="nazwisko" placeholder="Nazwisko" value="<?php echo set_value('nazwisko'); ?>">
<p class="help-block"><?php echo form_error('nazwisko'); ?></p>
</div>
</div>
<div class="form-group">
<label for="firma" class="col-sm-2 control-label">Firma:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firma" name="firma" placeholder="Firma" value="<?php echo set_value('firma'); ?>">
<p class="help-block"><?php echo form_error('firma'); ?></p>
</div>
</div>
<div class="col-sm-12" style="margin-bottom: 30px; margin-top: 30px;"> <h3>Wybierz interesujące Cię szkolenia</h3></div>
<div class="form-group">
<label class="col-sm-2 control-label">Szkolenia:</label>
<div class="col-sm-10">
<?php foreach ($szkolenia as $szkolenie): ?>
<div class="checkbox">
<label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo $szkolenie->id; ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
<br>
</div>
<?php endforeach; ?>
</div>
</div>
//CALLBACK CHECKBOX
<div class="form-group">
<div class="col-sm-12" style="margin-top: 50px;">
<input type="checkbox" name="accept_terms_checkbox" value="1"/> Zgadzam się na otrzymywanie maili od firmy Gamma<br>
<p><?php echo form_error('accept_terms_checkbox') ?></p>
</div>
</div>
<button type="submit" class="btn btn-lg btn-success" style="margin-top: 50px;">Odbierz Voucher</button>
<?php echo form_close(); ?>
In order to use your own callback method with codeigniter's validation class, you must declare the method. IN your case ( add this to your controller or a helper etc...):
public function accept_terms($user_input){
if($user_input != ""){
return True;
}else{
$this->form_validation->set_message('accept_terms_checkbo', 'The %s field can not be empty"');
return False;
}
I fixed this error by simple validation without callback function. I figure out that I don't need callback function for Terms of Services checkbox.
$this->form_validation->set_rules('accept_terms_checkbox', 'Checkbox', 'required');
$this->form_validation->set_message('required', 'You need to accept Terms of Services');

Codeigniter: no records has been saved in mysql database after clicking submit button

I'm currently working on codeigniter. I want to insert records in database. But when I click submit button, the records in database has not been saved.
Please help me. Thank you.
Here is my view (payroll_add.php):
<?php echo form_open('home/saveEmpPayroll',array('class'=>'form-horizontal'));?>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Select Employee</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<?php echo form_dropdown('empid', $dropdown, '', 'class="form-control" id="empid"'); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Basic Salary</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="emp_salary" name="emp_salary" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Total Bus Income for the week</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="total_income" name="total_income" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Bracket</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select class="form-control" id="emp_SSS">
<option value="<?php if (isset ($_POST['SSS_bracket'])) {
echo $_POST ['$SSS_bracket'];}?>"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">SSS Deduction</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="SSSdeduction" name="SSSdeduction" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Philhealth Bracket</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select class="form-control" id="emp_Philhealth">
<option value="<?php if (isset ($_POST['Philhealth_bracket'])) {
echo $_POST ['$Philhealth_bracket'];}?>"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Philhealth Deduction</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="Philhealthdeduction" name="Philhealthdeduction" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Type of Allowance</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" class="form-control" readonly="readonly" value="Meal Allowance">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Total Allowance</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" id="emp_allowance" name="emp_allowance" class="form-control" >
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button type="submit" class="btn btn-success" name="emp_submit" id="emp_submit">Submit</button>
</div>
</div>
</form>
Here is my controller: (home.php)
public function viewAddEmployeePayrollForm() {
$this->load->model('Model_payroll');
$data = array();
$data['dropdown'] = $this->Model_payroll->get_dropdown();
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('payroll/payroll_add', $data);
}
public function saveEmpPayroll() {
$this->load->model('Model_payroll');
$p = new Model_payroll();
$p->emp_id = $this->input->post('empid');
$p->basic_salary = $this->input->post('emp_salary');
$p->meal_allowance = $this->input->post('emp_allowance');
$p->SSS_bracket = $this->input->post('emp_SSS');
$p->SSS_deduction = $this->input->post('SSSdeduction');
$p->Philhealth_bracket = $this->input->post('emp_Philhealth');
$p->Philhealth_deduction = $this->input->post('Philhealthdeduction');
$p->bus_income = $this->input->post('total_income');
$result = $p->saveEmployeePayroll();
if (!$result) {
echo mysqli_error($result);
}
else {
redirect('home/goViewEmpPayroll', 'refresh');
}
}
Here is my model (model_payroll.php):
<?php
class Model_payroll extends CI_Model {
public $emp_id;
public $basic_salary;
public $meal_allowance;
public $SSS_bracket;
public $SSS_deduction;
public $Philhealth_bracket;
public $Philhealth_deduction;
public $ot_rate;
public $ot_total;
public $bus_income;
public function getEmployeePayroll() {
$this->db->select("*");
$this->db->from('tbl_payroll');
$this->db->join('employees', 'employees.empnum = tbl_payroll.emp_id');
$query = $this->db->get();
return $query->result();
}
public function addEmployeePayroll() {
$this->db->where('emp_id', $this->emp_id);
$query = $this->db->insert('tbl_payroll', $this);
return $query;
}
public function saveEmployeePayroll() {
if (isset($this->emp_id)) {
$query = $this->updateEmployeePayroll();
}
else {
$query = $this->addEmployeePayroll();
}
return $query;
}
public function updateEmployeePayroll() {
$this->db->where('emp_id', $this->emp_id);
$query = $this->db->update('tbl_payroll', $this);
return $query;
}
public function get_dropdown() {
$result = $this->db->select('empnum, name')->get('employees')->result_array();
$dropdown = array();
foreach($result as $r) {
$dropdown[$r['empnum']] = $r['name'];
}
return $dropdown;
}
}
But there's nothing records has been saved in mysql database after clicking submit button. What am I doing wrong?
In your method
public function updateEmployeePayroll() {
$this->db->where('emp_id', $this->emp_id);
$query = $this->db->update('tbl_payroll', $this);
return $query;
}
$query = $this->db->update('tbl_payroll', $this);
$this contain CI data as well. please do not use like that please see below for example.
https://ellislab.com/codeigniter/user-guide/database/active_record.html
$this->db->update();
Generates an update string and runs the query based on the data you supply. You can pass an array or an object to the function. Here is an example using an array:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
// Produces:
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
You must have to form_validation:
public function saveEmpPayroll() {
if($this->form_validation->run()){
$this->load->model('Model_payroll');
$p = new Model_payroll();
$p->emp_id = $this->input->post('empid');
$p->basic_salary = $this->input->post('emp_salary');
$p->meal_allowance = $this->input->post('emp_allowance');
$p->SSS_bracket = $this->input->post('emp_SSS');
$p->SSS_deduction = $this->input->post('SSSdeduction');
$p->Philhealth_bracket = $this->input->post('emp_Philhealth');
$p->Philhealth_deduction = $this->input->post('Philhealthdeduction');
$p->bus_income = $this->input->post('total_income');
$result = $p->saveEmployeePayroll();
if (!$result) {
echo mysqli_error($result);
}
else {
redirect('home/goViewEmpPayroll', 'refresh');
}
}
else{
redirect('controller/viewAddEmployeePayrollForm');
}
}

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

Uploading image from one function

I have a edit function on my users codeigniter project, I would like to be able to use that one function for uploading images as well but user guide says to create a new function.
How am I able to make it with same edit() function.
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
// User userfile
if(!empty($this->input->post('userfile'))) {
$data['userfile'] = $this->input->post('userfile');
} else {
$data['userfile'] = $this->model_user->getUserImage($user_id);
}
$config['upload_path'] = base_url('image/catalog/'); // base_url('image/catalog/');
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
} else {
$data['upload_data'] = $this->upload->data();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View File
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo $error ;?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $userfile;?>">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="userfile" value="<?php echo $userfile;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>
I have got it to work with out using codeigniter upload image helper easier this way.
controller function
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
if(!empty($this->input->post('image'))) {
$data['image'] = $this->input->post('image');
} else {
$data['image'] = $this->model_user->getUserImage($user_id);
}
$data['image_path'] = config_item('HTTP_CATALOG') . '/image/catalog/';
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $image_path . $image; ?>" style=" width: 100%; height: 300px;">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="image" value="<?php echo $image;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>

Categories