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();
Related
I am unable to upload a image in the destination folder in codeigniter, I created one controller under the controller/admin folder with a name called site. And I added one model under the model folder with a name called model_view, and finally I created one view in the view folder with a name called view_addsite. And I created one table in the mysql with a name called sitesettings and 14 columns are added in that table. Problem hear is all fields are added in the table except image. please find the code.
controller(site)::
===========
public function add()
{
$datte = date('Y-m-d H:i:s');
if(!$this->input->post('buttonSubmit'))
{
$data['message'] = '';
$this->load->view('admin/view_addsite', $data);
}
else
{
//$this->load->library('form_validation');
if($this->form_validation->run('addsite'))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$this->load->library('upload', $config);
$title = $this->input->post('title');
$name = $this->input->post('name');
$admin = $this->input->post('admin');
$mail = $this->input->post('mail');
$phone = $this->input->post('phone');
$contact=$this->input->post('contact');
$fb=$this->input->post('fb');
$tw=$this->input->post('tw');
$yt=$this->input->post('yt');
$in=$this->input->post('in');
$gp=$this->input->post('gp');
$ft=$this->input->post('ft');
$this->upload->do_upload('image');
$data = $this->upload->data('image');
$image= $data['file_name'];
$this>model_site>
insert($title,$image,$name,$admin,$mail,
$phone,$contact,$fb,$tw,$yt,$in,$gp,$ft);
$this->session->set_flashdata('message','site Successfully
Created.');
redirect(base_url('admin/site'));
}
else
{
$data['message'] = validation_errors();
$this->load->view('admin/view_addsite', $data);
}
}
}
Model(model_site)::
===================
public function
insert($title,$image,$name,$admin,$mail,$phone,$contact,
$fb,$tw,$yt,$in,$gp,$ft)
{
$data = array(
'admintitle' => $title,
'logo' => $image,
'fromname' => $name,
'adminemail'=> $admin,
'receivemail' => $mail,
'phonenumber' => $phone,
'contactaddress' => $contact,
'facebook' => $fb,
'twitter'=>$tw,
'youtube' => $yt,
'instagram' => $in,
'googleplus' => $gp,
'footer' => $ft,
);
$this->db->insert('sitesettings', $data);
}
view(view_addsite)::
===================
<!-- page content -->
<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_left">
<h3>Sitesettings</h3>
</div>
</div><div class="clearfix"></div>
<hr>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Add a new Site</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-
chevron-up"></i></a></li>
<li><a class="close-link"><i class="fa fa-close">
</i></a></li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<label><?php echo $message; ?></label>
<form method="post">
<fieldset>
<div class="form-group">
AdminPage Title : <input class="form-control"
placeholder="Admin Title" name="title" id="title"
type="text" ><span id="user-availability-status">
</span>
<div class="form-group">
Select image to upload:
<input type="file" name="fileToUpload"
id="fileToUpload">
</form>
<div class="form-group">
From Name : <input class="form-control"
placeholder="Form Title" name="name" id="name" type="text"
><span id="user-availability-status"></span>
<div class="form-group">
Admin Email Address : <input class="form-control"
placeholder="Admin Email" name="admin" id="admin"
type="text" onBlur="checkAvailability()" ><span
id="user-availability-status"></span>
<div class="form-group">
Receive Mail Address for Contact Us Form : <input
class="form-control" placeholder="Receive Email"
name="mail" id="mail" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Phone number: <input class="form-control"
placeholder="Phone Number" name="phone" id="phone"
type="text" onBlur="checkAvailability()" ><span id="user-
availability-status"></span>
</div>
<div class="form-group">
Contact Adress : <input class="form-control"
placeholder="Contact Address" name="contact" id="contact"
type="text" onBlur="checkAvailability()" ><span
id="user-availability-status"></span>
</div>
<div class="form-group">
Facebook : <input class="form-control" placeholder="Facebook"
name="fb" id="fb" type="text" onBlur="checkAvailability()" >
<span id="user-availability-status"></span>
</div>
<div class="form-group">
Twitter : <input class="form-control" placeholder="Twitter"
name="tw" id="tw" type="text" onBlur="checkAvailability()" >
<span id="user-availability-status"></span>
</div>
<div class="form-group">
Youtube : <input class="form-control"
placeholder="Youtube" name="yt" id="yt" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Instagram : <input class="form-control"
placeholder="Instagram" name="in" id="in" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Google Plus: <input class="form-control"
placeholder="Google Plus" name="gp" id="gp" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Footer: <input class="form-control"
placeholder="Footer" name="ft" id="ft"
type="text" onBlur="checkAvailability()" ><span id="user-
availability-status"></span>
</div>
<input type="submit" name="buttonSubmit"
value="add" class="btn btn-success" />
</fieldset>
</form>
</div> <!-- /content -->
</div><!-- /x-panel -->
</div> <!-- /col -->
</div> <!-- /row -->
</div>
</div> <!-- /.col-right -->
<!-- /page content -->
<?php $this->load->view('admin/partials/admin_footer'); ?>
<?php if($this->session->flashdata('message') != NULL) : ?>
<script>
swal({
title: "Success",
text: "<?php echo $this->session->flashdata('message'); ?>",
type: "success",
timer: 1500,
showConfirmButton: false
});
</script>
<?php endif ?>
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
for more detail see:upload file with codeigniter
You are missing
enctype="multipart/form-data"
on your form tag and also remove the </form> just after the image input box.
Controller:
$doc_1 = "uploads/img_1/";
if (isset($_FILES['userfile_1']) && !empty($_FILES['userfile_1'])) {
$filename = pathinfo($_FILES['userfile_1']['name']);
if (move_uploaded_file($_FILES['userfile_1']['tmp_name'], $doc_1.$filename['basename'])) {
$attch_1 = base_url().$doc_1.$filename['basename'];
}
else {
$attch_1 = "na";
}
}
view:
<?php echo form_open_multipart("store_property", ['class'=>'form-horizontal']); ?>
<input type="file" name="userfile_1" />
I can manage to insert data into the tblaccount. But the problem is the picture can't be uploaded. The tblaccount contains the right data of firstname, lastname, email, department, username, and password, but the picture remains blank even I've uploaded some images.
UPDATE: The picture can now be uploaded in the folder, but no luck in tblaccount. It only show blank data.
signup.php controller:
public function index()
{
// set form validation rules
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[tblaccount.Email]');
$this->form_validation->set_rules('department', 'Department', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[30]|is_unique[tblaccount.Username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]');
$this->form_validation->set_rules('picture', 'Image', 'trim|required');
// submit
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('signup_view');
}
else
{
if(!empty($_FILES['picture']['name']))
{
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000000;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture'))
{
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
}
else
{
$picture = '';
$error = array('error' => $this->upload->display_errors());
echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '".base_url("index.php/signup")."';</script>";
}
}
else
{
$picture = '';
}
$data = array(
'First_Name' => $this->input->post('firstname'),
'Last_Name' => $this->input->post('lastname'),
'Email' => $this->input->post('email'),
'Department' => $this->input->post('department'),
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'Picture' => $picture
);
if ($this->account_model->insert($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>');
redirect('login');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('signup');
}
}
}
signup.php view:
<div class="row">
<div class="col-md-4 col-md-offset-4 well">
<?php echo form_open_multipart('signup');?>
<legend>Signup</legend>
<div class="form-group">
<label for="name">First Name</label>
<input class="form-control" name="firstname" placeholder="First Name" type="text" value="<?php echo set_value('First_Name');?>"/>
<span class="text-danger"><?php echo form_error('firstname'); ?></span>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input class="form-control" name="lastname" placeholder="Last Name" type="text" value="<?php echo set_value('Last_Name');?>"/>
<span class="text-danger"><?php echo form_error('lastname'); ?></span>
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input class="form-control" name="email" placeholder="Email Address" type="text" value="<?php echo set_value('Email');?>"/>
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="email">Department</label>
<input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
<span class="text-danger"><?php echo form_error('department'); ?></span>
</div>
<div class="form-group">
<label for="email">Username</label>
<input class="form-control" name="username" placeholder="Username" type="text" value="<?php echo set_value('Username');?>"/>
<span class="text-danger"><?php echo form_error('username'); ?></span>
</div>
<div class="form-group">
<label for="subject">Password</label>
<input class="form-control" name="password" placeholder="Password" type="password"/>
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group">
<label for="subject">Confirm Password</label>
<input class="form-control" name="cpassword" placeholder="Confirm Password" type="password"/>
<span class="text-danger"><?php echo form_error('cpassword'); ?></span>
</div>
<div class="form-group">
<label for="subject">Profile Picture:</label>
<input class="form-control" name="picture" accept="image/*" type="file"/>
<span class="text-danger"><?php echo form_error('picture'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-info">Signup</button>
<button name="cancel" type="reset" class="btn btn-info">Cancel</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
Remove required from validation check as it is not allowed to pass in image field ,when you check empty file field (!empty($_FILES['picture']['name']) then required validation already checked inside it , Secondly you have to check whether the directory is created or not and provide 777 permission to it . I have tested code by adding only these two check. Hope it help
$this->form_validation->set_rules('picture', 'Image', 'trim'); /*Change in this line -- remove required*/
if ($this->form_validation->run()) {
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = 'uploads/images/';
/*add 777 permission to directory*/
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path'], 0777, TRUE);
}
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000000;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('picture')) {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
} else {
$picture = '';
$error = array('error' => $this->upload->display_errors());
echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '" . base_url("index.php/signup") . "';</script>";
}
} else {
$picture = '';
}
$data = array(
'image' => $picture
);
$this->write_conn->db->insert('test', $data);
if ($this->write_conn->db->insert('test', $data)) {
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>');
} else {
// error
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
}
}
On your upload path
$config['upload_path'] = 'uploads/images/';
Try
$config['upload_path'] = './uploads/images/';
$config['upload_path'] = FCPATH . '/uploads/images/';
And I use form_open_multipart() form helper make sure your folder correct permissions
On another note make sure you have named your file and class name correct where signup.php would be Signup.php Only the first letter should be uppercase on class and filename explained here https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming
<?php
class Signup extends CI_Controller {
}
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');
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']);
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();?>