What is the cause of this wrong image upload error message in my Codeigniter 3 application? - codeigniter

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
There is, among others an "Edit account information" form, which has an image upload field. In the controller, the update() method contains the logic for the image upload action:
public function update() {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
// Upload avatar
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '100';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$uerrors = array('uerrors' => $this->upload->display_errors());
$data['uerrors'] = $uerrors;
}
if(!$this->form_validation->run() || !empty($uerrors))
{
print_r($data['uerrors']);
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else
{
$this->Usermodel->update_user($id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
The (surprising) problem I have is that, even though I am uploading an image, print_r($data['uerrors']); returns You did not select a file to upload. in the browser.
In the view, the same error is returned:
<?php echo form_open(base_url('dashboard/users/update')); ?>
<input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo set_value('first_name', $author->first_name); ?>" placeholder="First name">
<?php if(form_error('first_name')) echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo set_value('last_name', $author->last_name); ?>" placeholder="Last name">
<?php if(form_error('last_name')) echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
<textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo set_value('bio', $author->bio); ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<input type="hidden" name="avatar" id="avatar" value="<?php echo $author->avatar; ?>">
<label for="avatar">Upload avatar</label>
<div class="form-group">
<input type="file" name="userfile" id="uavatar" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
<div class="form-group">
<div class="w-50 pull-left pr-1">
<input type="submit" value="Update" class="btn btn-block btn-md btn-success">
</div>
<div class="w-50 pull-right pl-1">
Cancel
</div>
</div>
<?php echo form_close(); ?>
The error message I was expecting, considering that the image I was trying to upload is larger then the specified limit (100KB) is: The file you are attempting to upload is larger than the permitted size.
What am I doing wrong?

Try this
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
<div class="form-group">
<input type="file" name="userfile" id="userfile" size="20">
<p><?php print_r($uerrors); ?></p>
</div>

Try to change your form opening, from :
<?php echo form_open(base_url('dashboard/users/update')); ?>
to
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
To change the form encoding type from text/plain to multipart/form-data to support image data upload. Here is the difference between each encoding type.

Related

How can I get the role name

<?php
$role_class = new RoleModel;
$role_name = "";
foreach ($role_class->getInfoById("role_id") as $row) {
# code...
$role_name = $row->role_name;
}
?>
<fieldset class="g12 go pb_s">
<legend>Basic Info</legend>
<div class="g12 go">
<div class="g7 go">
<div class="g2">
<label for="groupName">Role Name:</label>
</div>
<div class="g4">
<input type="text" placeholder="Type Role Name here..." name="role_name" value="<?php echo $role_name; ?>" required="required">
</div>
</div>
</div>
</fieldset>
You can use PHP's $post to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
In your Html
<form name="form" action="" method="post">
<input type="text" name="role_name" id="role_name" value="<?php echo $role_name;?>">
</form>
In your .php, you can get the value like below
$role_name=$post['role_name'];
Since the editor edited his questions, i will add more example.
For example, i got a list of data want to show in the html, in controller, i will put into an array first.
$form_fields = array('username', 'ref', 'password', 'password_confirm')
after that in html, i will do like this
<?php foreach($__form_fields as $field) {?>
<input type="text" placeholder="<?php echo $field?>" name="<?php echo $field? >" value="" required="required">
<?php }?>
which the above code will loop and the input field will have he name

Generating admission number using id in MYSQL. I want code from CodeIgniter

In my project, I have a student id no. It should auto increment. I want to auto-generate an admission number. How do I do it?
This is my controller
$insert_id = $this->student_model->add($data);
$data_new = array(
'student_id' => $insert_id,
'class_id' => $class_id,
'section_id' => $section_id,
This is my model
public function add($data) {
if (isset($data['id'])) {
$this->db->where('id', $data['id']);
$this->db->update('students', $data);
} else {
$this->db->insert('students', $data);
return $this->db->insert_id();
}
}
Here is my view
<form id="form1" action="<?php echo site_url('student/create') ?>" id="employeeform" name="employeeform" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<div class="box-body">
<div class="tshadow mb25 bozero">
<h4 class="pagetitleh2"><?php echo $this->lang->line('student'); ?> <?php echo $this->lang->line('admission'); ?> </h4>
<div class="around10">
<?php if ($this->session->flashdata('msg')) { ?>
<?php echo $this->session->flashdata('msg') ?>
<?php } ?>
<?php echo $this->customlib->getCSRF(); ?>
<input type="hidden" name="sibling_name" value="<?php echo set_value('sibling_name'); ?>" id="sibling_name_next">
<input type="hidden" name="sibling_id" value="<?php echo set_value('sibling_id',0); ?>" id="sibling_id">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label for="exampleInputEmail1"><?php echo $this->lang->line('admission_no'); ?></label> <small class="req"> *</small>
<input autofocus="" id="admission_no" name="admission_no" placeholder="" type="text" class="form-control" value="<?php echo set_value('admission_no', date($this->customlib->getSchoolDateFormat())); ?>" name="admission_no" class="form-control">
<span class="text-danger"><?php echo form_error('admission_no'); ?></span>
</div>
Here admission number put manually, but I want it auto-created. I don't know how to do. I'm new with CodeIgniter.
My database
My form view
if you want to add unique admission you must chick the admission or use this query
$this->db->order_by('admission_no','DESC');
$this->db->get('students',1)->row()->admission_no + 1;
Put the bellow code in your view in your admission input which id of it is id="admission_no" romove the input and copy paste the code. this get the lastest admission no and then add 1 to the admission no also you can remove this value and add manually another value. that must be unique
<?php
$this->db->order_by('admission_no','DESC');
$auto-add = $this->db->get('students',1)->row()->admission_no + 1;
?>
<input autofocus="" id="admission_no" name="admission_no" placeholder="" type="text" class="form-control" value="<?php echo $auto-add; ?>" class="form-control">

Trying to fetch image from database in codeigniter

I was uploaded Image in database Successfully. But not succeed to fetch image from database.
i want to fetch the image from database.
here is my code,
controller -> login.php
public function user_update(){
$this->load->model('login_model');
$this->form_validation->set_rules('fname', 'First Name','required');
$this->form_validation->set_rules('lname', 'Last Name','required');
$this->form_validation->set_rules('cnumber', 'Contact Number','required|min_length[10]|max_length[10]|numeric');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf|gif|jpg|png|xlsx';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (( ! $this->upload->do_upload('image')) && ($this->form_validation->run() === FALSE ))
{
$this->load->view('student/home');
}
else
{
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url().$image_name;
$udata = array(
'id' => $this->input->post('id'),
'fname' => $this->input->post('fname'),
'mname' => $this->input->post('mname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'cnumber' => $this->input->post('cnumber'),
'image'=> $resume
);
$this->login_model->Updateuser($udata);
}
}
model file:- login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login_model extends CI_Model
{
function Updateuser($param) {
$id = $param['id'];
$usar1 = array(
'Student_Name' => $param['fname'],
'm_name' => $param['mname'],
'l_name' => $param['lname'],
'Student_Email' => $param['email'],
'contact_no' => $param['cnumber'],
'Address' => $param['address'],
'image' => $param['image']
);
$this->db->where('Student_id', $id);
$this->db->update('students', $usar1);
//echo ($this->db->last_query());
//exit();
$sturesult = $this->login_model->get_student_list();
$data['Stulist'] = $sturesult;
$this->load->view('student/default_list',$data);
}
}
view file:- edit_info.php
<div class="container" id='main-layout' style="border-top: 1px solid #D14B54; background: #f5f5f5f5">
<div class="row">
<div class="col-md-8 col-sm-8">
<div class="ajaxResponse"><input type="hidden" name="ajaxResponse"></div>
<div class="row" style="padding: 10px 5px;">
<?php $id = $this->uri->segment(3); if(!empty($id)): ?>
<div class="">
<?php echo validation_errors();
echo $lname;
?>
<div class="thumbnail familycol" style="padding:16px">
<?php echo form_open_multipart('login/user_update'); ?>
<legend>Personal Information</legend>
<div class="row">
<?php //echo form_label('Id :'); ?> <?php echo form_error('id'); ?>
<input type="hidden" name="id" value="<?php echo $row->Student_id; ?>" class="form-control input-sm" placeholder="id"/>
<!-- <label class="required">First name</label>-->
<div class="col-xs-6 col-md-6">
<label for="First Name">First Name:</label>
<input type="input" name="fname" class="form-control" value="<?php echo $row->Student_Name;?>"/><font color="red"><?php echo form_error('fname'); ?></font>
</div>
<div class="col-xs-6 col-md-6">
<!--<label class="required">Middle name</label>-->
<label for="Last Name">Middle Name:</label>
<input type="input" name="mname" class="form-control" value="<?php echo $row->m_name?>"/><font color="red"><?php echo form_error('mname'); ?></font>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-6">
<!--<label class="required">Last name</label>-->
<label for="Last Name">Last Name:</label>
<input type="input" name="lname" class="form-control" value="<?php echo $row->l_name ;?>"/><font color="red"><?php echo form_error('lname'); ?></font>
</div>
<div class="col-xs-6 col-md-6">
<?php echo form_label('Email :'); ?> <?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo $row->Student_Email; ?>" class="form-control input-sm" placeholder="Email" />
</div>
</div>
<br>
<div class="row">
<div class="col-sm-6">
<!--<label>Address</label>-->
<label for="text">Address</label>
<textarea name="address" class="form-control"><?php echo $row->Address ;?></textarea><font color="red"><?php echo form_error('address'); ?></font>
</div>
<div class="col-sm-6">
<label for="text">Contact Number</label>
<textarea name="cnumber" class="form-control"><?php echo $row->contact_no ;?></textarea><font color="red"><?php echo form_error('cnumber'); ?></font>
</div>
<br/>
</div>
<br/>
<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="image" accept="image/*" value="upload">
<br/>
<button class="btn btn-lg btn-primary btn-block signup-btn" type="submit" style="margin-top: 5px;">
Update my Profile</button>
</div>
</div>
<?php endif;?>
</div>
</div>
</div>
</div>
Ok Lets Put Code in your View file:
<img src="<?php echo base_url('uploads/').$Stulist[$i]->image; ?>"
You have updated the wrong image directory of uploaded images in database.
Simply update the following lines of code
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url().$image_name;
to
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url('uploads').$image_name;
here the base_url points to the uploads directory where you have uploaded your images.

Validate with codeigniter

I dont know what am i doing wrong with the validation.
here is my Controller
function update_user() {
$this->load->library('form_validation');
$this->form_validation->set_rules('sirname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('name', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('update_view');
}
else
{
$data = array(
'surname' => $this->input->post('sirname'),
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
);
$this->Account_model->update_user(31,$data);
$this->show_user();
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Updated!</div>');
redirect('home');
}
}
here is my view
<form method="post" action="<?php echo base_url() . "account/update_user"?>" >
<div class="form-group">
<label for="name">First Name</label>
<input class="form-control" name="sirname" placeholder="Your First Name" type="text" value="<?php echo $user->surname; ?>" />
<span class="text-danger"><?php echo form_error('sirname'); ?></span>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input class="form-control" name="name" placeholder="Last Name" type="text" value="<?php echo $user->name; ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo $user->email; ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-default" onclick="account.php">Update</button>
<button name="cancel" type="reset" class="btn btn-default">Cancel</button>
</div>
<?php echo form_close(); ?>
<?php endforeach; ?>
<?php echo $this->session->flashdata('msg'); ?>
i need to display the value already in the database as im doing an update info for user.. so how do i implement the set_value('name') as well.
First preference is for form_error(),
so in value for each input fields,
value="<?php form_error("surname") ? echo set_value("surname") : echo $user->surname; ?> ?>"
Which means, if form error, then echo set_value() else echo database value.
in home controller where you redirect to success load :
$this->load->library('form_validation');
it will be ok!
You can use CI's built-in set_value function, which lets you set the values of a form field. It has a second (optional) parameter, to set the default value for the field.
<? echo form_label('Last Name', 'name'); ?>
<input class="form-control" name="name" id="name" type="text" value="<? echo set_value('name', $user->name) ?>">
<? echo form_error('name', '<span class="text-danger">','</span>');?>
In the code above, when loaded for the first time, the form will show the value from the database. But when returned after failing form validation, it will show the user input.

codeigniter submit ajax alert success

i have this form that submits personal details
here is my view
<?php $attributes=a rray( 'autocomplete'=>"off", 'id' => 'theForm');?>
<?php echo form_open_multipart( 'employee/personaldetails', $attributes); ?>
<div class="row">
<div class="span3 offset1">
<label>First Name</label>
<input onkeypress="return fnAllowAlpha(event);" maxlength="50" type="text" value="<?php echo $info['first_name']; ?>" name="first_name" disabled>
<input type="hidden" value="<?php echo $info['employee_image']; ?>" name="employee_image" disabled>
</div>
<div class="span3">
<label>Middle Name</label>
<input onkeypress="return fnAllowAlpha(event);" maxlength="50" type="text" value="<?php echo $info['middle_name']; ?>" name="middle_name" disabled>
</div>
<div class="span3">
<label>Last Name</label>
<input onkeypress="return fnAllowAlpha(event);" maxlength="50" type="text" value="<?php echo $info['last_name']; ?>" name="last_name" disabled>
</div>
<div class="span1">
<div class="down1">
<button type="submit" class="button-orange" id="btnSave" style="display:none;">Save</button>
</div>
</div>
CONTROLLER
$this->load->model('mdl_employee','emp');
$id = $this->session->userdata('id');
$P1 = $this->input->post('first_name');
$P2 = $this->input->post('middle_name');
$P3 = $this->input->post('last_name');
$this->emp->update_myinfo($id, $P1, $P2, $P3);
model
public function update_myinfo($id, $P1, $P2, $P3){
$employee_data = array(
'first_name' => $P1,
'middle_name' => $P2,
'last_name' => $P3);
$this->db->where('employee_id', $id);
$this->db->update('employee', $employee_data);
}
i dont know how to use ajax.
i want to alert the ajax if success or not. after updating my database.. im just new programmer
Maybe you could try googling since its so common to submit forms using ajax.
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Submitting HTML form using Jquery AJAX
I would suggest first try these links..and if u still get stuck then let us know, we would surely help you :)

Resources