Unable to retrieve ID to update in database - php

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

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.

Image uploading in codeigniter in database

I have a problem with uploading images in the database in codeigniter. When I upload image in the database, it shows this type of error like:
The upload path does not appear to be valid. So what's the problem in this code. I cannot figure out, so please help me.
I tried base_url() method also but error is not solved...!
My Htmlcode:-
<?php echo form_open_multipart('student/insertstudent') ?>
<form>
<div class="panel panel-primary" >
<div class="panel-heading" >
<p class="label" style="font-size: 15px">Student Registration Form</p>
</div>
<div class="panel-body">
<fieldset>
<div class="form-group">
<label for="exampleInputEmail1">First Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter First Name" name="fname" value="<?php echo set_value('fname'); ?>">
<div class="row-lg-6">
<?php echo form_error('fname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Last Name</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Last Name" name="lname" value="<?php echo set_value('lname'); ?>">
<div class="row-lg-6">
<?php echo form_error('lname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleTextarea">Address</label>
<textarea class="form-control" id="exampleTextarea" rows="2" placeholder="Enter Address" name="add"><?php echo set_value('add'); ?></textarea>
<div class="row-lg-6">
<?php echo form_error('add'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Contact Number</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Contact Number" name="cno" value="<?php echo set_value('cno'); ?>">
<div class="row-lg-6">
<?php echo form_error('cno'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Emaid ID</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter your email" name="email" value="<?php echo set_value('email'); ?>">
<div class="row-lg-6">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Enter Course</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="cname" value="<?php echo set_value('cname'); ?>">
<div class="row-lg-6">
<?php echo form_error('cname'); ?>
</div>
</div>
<!-- <div class="form-group">
<label for="exampleInputPassword1">Select Student Photo</label>
<input type="file" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="photo">
<div class="row-lg-6">
<?php echo form_error('photo'); ?>
</div>
</div> -->
<div class="form-group">
<label for="exampleInputPassword1">Select Image</label>
<?php echo form_upload(['name' => 'userfile','class'=>'form-control']); ?>
<div class="row-lg-6">
<?php if(isset($upload_error))
{
echo $upload_error;
} ?>
</div>
</div>
<h4 align="center" style="margin-left: -150px"><button type="submit" class="btn btn-primary">Register</button></h4>
<h4 align="center" style="margin-top: -43px;margin-left: 200px"><button type="reset" class="btn btn-primary">Reset</button></h4>
</fieldset>
</div>
</div>
my controller file:-
public function insertstudent()
{
$this->form_validation->set_rules('fname', 'FirstName', 'required|alpha');
$this->form_validation->set_rules('lname', 'lastName', 'required|alpha');
$this->form_validation->set_rules('add', 'Address', 'required');
$this->form_validation->set_rules('cno', 'Contact Number', 'required|numeric');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('cname', 'Course Name', 'required');
// $this->form_validation->set_rules('userfile', 'Student Photo', 'required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");
$config['upload_path'] = base_url('asset/uploads/');
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);
$this->load->library('upload',$config);
if ($this->form_validation->run() == TRUE && $this->upload->do_upload('userfile')) {
$post = $this->input->post();
$data = $this->upload->data();
$image_path = base_url("upload/".$data['raw_name'].$data['file_ext']);
$post['image_path'] = $image_path;
$this->load->model('useradd');
if($this->useradd->addstudent($post)){
$this->session->set_flashdata('success', 'Record Successfully Inserted');
return redirect('admin');
}else {
$this->load->view('addstudent');
}
// $data = array(
// 'firstname' => $this->input->post('fname'),
// 'lastname' => $this->input->post('lname'),
// 'address' => $this->input->post('add'),
// 'phone' => $this->input->post('cno'),
// 'email' => $this->input->post('email'),
// 'course' => $this->input->post('cname'),
// 'photo' => $image_path,
// );
// // $this->load->library('upload', $data);
// if ($result == true) {
// $this->session->set_flashdata('success', 'Record Successfully Inserted');
// return redirect('admin');
// } else {
// $this->load->view('addstudent');
// }
} else {
$upload_error = $this->upload->display_errors();
$this->load->view('addstudent',compact('upload_error'));
}
}
my Model:-
class useradd extends CI_Model
{
function addstudent($data)
{
$result = $this->db->insert('add_student', $data);
}
}
My error is:
The upload path does not appear to be valid.
Try This Code To Upload Image.and remove form tag in your view file and you didn't define form_close method at the end of the form of your view file
$file = $_FILES['filename']['name'];
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload();
$allData = $this->upload->data();

form validation and record insert not working in codeigniter

Form validation and record insert not working in codeigniter
model
class Mymodel extends CI_Model
{
function insert($data)
{
$this->db->insert("users", $data);
}
}
controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_controller
{
function index()
{
$this->load->view('myfolder/my_page');
}
function login()
{
$this->load->view('myfolder/login');
}
function signup()
{
$this->load->view('myfolder/signup');
}
function signupmethod()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("firstname","First Name",'requird|alpha');
$this->form_validation->set_rules("lastname","Last Name",'requird|alpha');
$this->form_validation->set_rules("email","Email",'requird|alpha');
$this->form_validation->set_rules("password","Password",'requird|alpha');
$this->form_validation->set_rules("mobile","Mobile",'requird|alpha');
if ($this->form_validation->run())
{
$this->load->model("mymodel");
$data = array(
"firstname" => $this->input->post("firstname"),
"lastname" => $this->input->post("lastname"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"mobile" => $this->input->post("mobile"),
"curtime" => "NOW()"
);
if($this->input->post("Signup"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
}
}
public function Inserted()
{
$this->index();
}
}
?>
view(html code)
<?php include('header.php'); ?>
<form method="post" action="<?php echo base_url()?>mycontroller/signupmethod" enctype="multipart/form-data">
<div class="container">
<div class="row">
<h3>Login</h3>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>First Name</label>
<input type="text" name="firstname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("firstname"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Last Name</label>
<input type="text" name="lastname" value="" class="form-control">
<span class="text-danger"><?php echo form_error("lastname"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Email</label>
<input type="text" name="email" value="" class="form-control">
<span class="text-danger"><?php echo form_error("email"); ?></span>
</div>
<div class="col-md-6 form-group">
<label>Password</label>
<input type="password" name="password" value="" class="form-control">
<span class="text-danger"><?php echo form_error("password"); ?></span>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" name="mobile" value="" class="form-control">
<span class="text-danger"><?php echo form_error("mobile"); ?></span>
</div>
<div class="col-md-6 form-group" style="margin-top: 23px;">
<input type="submit" name="submit" value="Signup" class="btn btn-info">
</div>
</div>
I have read every line carefully, code run but no errors showing and validation and record insert are not working. and no errors showing.
please help.
Change controller this block of code when you fetch form elements in controller always use the element name
if($this->input->post("submit"))
{
$this->mymodel->insert($data);
redirect(base_url() . "mycontroller/Inserted");
}
Correct spelling of required
$this->form_validation->set_rules("firstname","First Name",'required|alpha');
Your set_rules statements are incorrect. Use required instead of requird otherwise set_rules will fail.
When you are not sure why form_validation is failing, try getting the errors with:
If($this->form_validation->run() == false)
{
echo validation_error();
}

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

Categories