update record with the form - Codeigniter 2.1 - codeigniter

i have some problem with update in CI 2.1
I followed the user guide "mini-tut" for create e news, but i cant understand how to update a record with a form.
my update models is:
// update dei record
public function update_news($id)
{
$data = array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('slug'),
'text' => $this->input->post('text')
);
$this->db->where('id', $id);
$this->db->update('news', $data);
}
how can i make the controller for update?? i try:
public function update($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update an intem';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/update');
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news($id);
$this->load->view('news/success');
}
}
but i display a 404() page...
the views for update is:
<h2>Update an item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/update') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="slug">Slug</label>
<input type="input" name="slug" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Update an item" />
</form>
any one can help me how do a "simnple" update for understand a CI logic?

If I understand what you want, then you should just pass the $this->input->post(x) to the model from your controller.
I personally have been using it like this:
Controller:
$data = array(
'title' => $this->input->post('title'),
'text' => $this->input->post('text'),
'slug' => $this->input->post('slug'),
);
if($this->my_model->exists($id)) {
$this->my_model->update($id, $data);
} else {
$this->my_model->insert($data);
}
And your Model should look like:
// update dei record
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update('news', $data);
}
Your controller uses the first segment as an argument of the controller method:
public function update($id)
You can also try using
$id = $this->uri->segment(3);

//Controller
public function update($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update an intem';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
$data['news'] = $this->news_model->get_news_by_id($id);
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/update', $data);
$this->load->view('templates/footer');
}
else
{
$this->load->helper('url');
$this->news_model->update_news($id);
redirect('/news', 'refresh');
}
}
//Model
public function update_news($id)
{
$data = array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('title'),
'text' => $this->input->post('text')
);
$this->db->where('id', $id);
$this->db->update('news', $data);
}
View
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $news[0]['title'];?>" /><br />
<input type = "hidden" name="id" value="<?php echo $news[0]['id'];?>" />
<label for="text">Text</label>
<textarea name="text"><?php echo $news[0]['text'];?></textarea><br />
<input type="submit" name="submit" value="Create news item" />

To add a page in code igniter
I will give you the example which i have done
Model:
function add()
{
$name=$_POST['name'];
$department=$_POST['dept'];
$degree=$_POST['degree'];
$mark=$_POST['mark'];
$this->db->query("INSERT INTO `simple`(name,department,degree,mark) VALUES('$name','$department','$degree','$mark')");
}
Controller:
public function add()
{
$this->load->model('modd');
$this->modd->add();
$this->load->view('add');
}
Views :
add.php
<form method="post" action="<?php base_url();?>index.php/contr/add">
<table>
<?php echo form_open('contr/add');?>
<tr>
<th>Name</th>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<th>Department</th>
<td><input type="text" name="dept" /></td>
</tr>
<tr>
<th>Degree</th>
<td><input type="text" name="degree" /></td>
</tr>
<tr>
<th>Marks</th>
<td><input type="text" name="mark" /></td>
</tr>
<tr>
<td><input type="submit" name="add" value="Add" /></td>
<?php echo form_close(); ?>
<td>
<input type="button" value="view" />
</td>
</tr>
</table>

Related

How to loop through a controller based on product_id in Laravel

I have these models in my Laravel-5.8
Product Model:
class Product extends Model
{
protected $fillable = [
'id',
'name',
];
public function invoice(){
return $this->belongsToMany('App\Invoice');
}
}
Invoice Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
protected $fillable = [
'id',
'customer_id',
'product_id',
'invoice_date',
'qty',
'price',
];
public function customer(){
return $this->belongsTo('App\Customer');
}
public function product(){
return $this->belongsToMany('App\Product');
}
}
Each invoice has one or more products.
Invoice Controller
public function create()
{
$customers = Customer::all();
$products = Product::all();
return view('invoice.create', compact('customers','products'));
}
public function store(Request $request)
{
$request->validate([
'customer_id' => 'required',
'product_id' => 'required',
'qty' => 'required',
'price' => 'required',
]);
$invoice = new Invoice();
$invoice->customer_id = $request->customer_id;
.....
return redirect('invoice/'.$invoice->id)->with('message','invoice created Successfully');
}
Invoice: create.blade
<form method="POST" action="{{route('invoice.store')}}">
#csrf
<div class="form-group col-md-3">
<label class="control-label">Customer Name</label>
<select name="customer_id" class="form-control">
<option>Select Customer</option>
#foreach($customers as $customer)
<option name="customer_id" value="{{$customer->id}}">{{$customer->name}} </option>
#endforeach
</select> </div>
<div class="form-group col-md-3">
<label class="control-label">Date</label>
<input name="invoice_date" class="form-control datepicker" value="<?php echo date('Y-m-d')?>" type="date" placeholder="Enter date">
</div>
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Product Name</th>
<th scope="col">Qty</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td><select name="product_id[]" class="form-control productname" >
<option>Select Product</option>
#foreach($products as $product)
<option name="product_id[]" value="{{$product->id}}">{{$product->name}}</option>
#endforeach
</select></td>
<td><input type="text" name="qty[]" class="form-control qty" ></td>
<td><input type="text" name="price[]" class="form-control price" ></td>
</tr>
</tbody>
</table>
<div >
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
From the diagram below:
Product_id, qty, price are arrays.
I want the controller to iterate and count the number of products, then the rows of product_id, qty and price in the view will be based on count of products. And then everything will be saved in the invoice table.
How do I complete my store controller code to achieve this?
Try this :
$products = $request->get('product_id');
$qte = $request->get('qty');
$price = $request->get('price');
foreach($products as $key=>$product){
$invoice =Invoice::creeate([
'product_id' => $product,
'qte' => $qte[$key],
'price' => $price[$key],
...
]);
}

Laravel - Dynamic input form duplicates record during update

I am trying to create Dynamic input form array using Laravel-5.8
I have these two models:
class HrLeaveType extends Model
{
protected $table = 'hr_leave_types';
protected $fillable = [
'company_id',
'leave_type_name',
'no_of_days',
'leave_type_code',
];
}
class HrLeaveTypeDetail extends Model
{
protected $table = 'hr_leave_type_details';
protected $fillable = [
'id',
'leave_type_id',
'company_id',
'employment_type_code',
'no_of_days',
];
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType', 'leave_type_id', 'id');
}
public function employmenttype()
{
return $this->belongsTo('App\Models\Hr\HrEmploymentType', 'employment_type_code', 'employment_type_code' );
}
}
Request Rules:
class UpdateLeaveTypeRequest extends FormRequest
{
public function rules()
{
'leave_type_name' =>
[
'required',
Rule::unique('hr_leave_types')->where(function ($query) {
return $query
->where('leave_type_name', 1)
->where('company_id', 1);
})->ignore($this->leave_type)
],
'leave_type_code' => [
'nullable',
'string',
'max:10',
],
'no_of_days' => 'required|array',
'no_of_days.*' => [
'required',
'numeric',
'max:120'
],
'employment_type_code' => 'required|array',
'employment_type_code.*' => [
'required',
],
];
}
}
And then the Controller:
public function edit($id)
{
$userCompany = Auth::user()->company_id;
$leavetype = HrLeaveType::findOrFail($id);
$employmenttypes = HrEmploymentType::where('company_id', $userCompany)->get();
$leavetypedetails = HrLeaveTypeDetail::where('leave_type_id', $id)->get();
return view('leave.leave_types.edit')
->with('leavetype', $leavetype)
->with('leavetypedetails', $leavetypedetails)
->with('employmenttypes', $employmenttypes);
}
public function update(UpdateLeaveTypeRequest $request, $id)
{
DB::beginTransaction();
try {
$leavetype = HrLeaveType::findOrFail($id);
$leavetype = new HrLeaveType();
$leavetype->leave_type_name = $request->leave_type_name;
$leavetype->leave_type_code = $request->leave_type_code;
$leavetype->description = $request->description;
$leavetype->save();
HrLeaveTypeDetail::where('leave_type_id', $id)->delete();
foreach ( $request->employment_type_code as $key => $employment_type_code){
$leavetypedetail = new HrLeaveTypeDetail();
$leavetypedetail->no_of_days = $request->no_of_days[$key];
$leavetypedetail->employment_type_code = $request->employment_type_code[$key];
$leavetypedetail->leave_type_id = $leavetype->id;
$leavetypedetail->save();
}
DB::commit();
Session::flash('success', 'Leave Type is updated successfully');
return redirect()->route('leave.leave_types.index');
}
catch (\Illuminate\Database\QueryException $e){
DB::rollback();
$errorCode = $e->errorInfo[1];
if($errorCode == 1062){
Session::flash('error', 'Duplicate Entry! Please try again');
}
return back();
}
catch (Exception $exception) {
DB::rollback();
Session::flash('error', 'Leave Type update failed!');
return redirect()->route('leave.leave_types.index');
}
}
view blade
<form action="{{route('leave.leave_types.update', ['id'=>$leavetype->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Name:<span style="color:red;">*</span></label>
<input type="text" name="leave_type_name" placeholder="Enter leave type name here" class="form-control" value="{{old('leave_type_name',$leavetype->leave_type_name)}}">
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Code:</label>
<input type="text" name="leave_type_code" placeholder="Enter leave type code here" class="form-control" value="{{old('leave_type_code',$leavetype->leave_type_code)}}">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Description</label>
<textarea rows="2" name="description" class="form-control" placeholder="Enter Description here ..." value="{{old('description',$leavetype->description)}}">{{old('description',$leavetype->description)}}</textarea>
</div>
</div>
<div class="col-sm-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Employment Type<span style="color:red;">*</span></th>
<th scope="col">Leave Days<span style="color:red;">*</span></th>
<th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
</tr>
</thead>
<tbody>
#foreach($leavetypedetails as $leavetypedetail)
<tr>
<td width="60%">
<select class="form-control select2bs4" data-placeholder="Select Employment Type" tabindex="1" name="employment_type_code[]">
<option name="employment_type_code[]" value="{{$leavetypedetail->employmenttype->employment_type_code}}">{{$leavetypedetail->employmenttype->employment_type_name}}</option>
#foreach($employmenttypes as $employmenttype)
<option name="employment_type_code[]" value="{{$employmenttype->employment_type_code}}">{{$employmenttype->employment_type_name}}</option>
#endforeach
</select>
</td>
<td width="35%"><input value="{{$leavetypedetail->no_of_days}}" type="text" name="no_of_days[]" class="form-control" max="120"></td>
<td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ trans('global.update') }}</button>
<button type="button" onclick="window.location.href='{{route('leave.leave_types.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('.addRow').on('click', function () {
addRow();
});
function addRow() {
var addRow = '<tr>\n' +
' <td width="60%"><select class="form-control select2bs4" data-placeholder="Choose Employment Type" tabindex="1" name="employment_type_code[]">\n' +
' <option value="0" selected="true" disabled="true">Select Employment Type</option>\n' +
' #if($employmenttypes->count() > 0 )\n' +
' #foreach($employmenttypes as $employmenttype)\n' +
' <option value="{{$employmenttype->employment_type_code}}">{{$employmenttype->employment_type_name}}</option>\n' +
' #endforeach\n' +
' #endif\n' +
' </select></td>\n' +
' <td width="35%"><input type="number" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120"></td>\n' +
' <td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
' </tr>';
$('tbody').append(addRow);
addRemoveListener();
};
addRemoveListener();
});
When I submitted the form for update,
I observed that the application save another data (duplicated) the model HrLeaveType into the database.
It replace the value of employment_type_code in the HrLeaveTypeDetail model with the latest row in HrLeaveType.
Why am I having these issues and how do I resolve it?
Thanks

I want to upload an image with first name and last name without refreshing the page in codeigniter

This is my controller
function edit_profile() {
// if (isset($_POST['submit']))
//{
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$name = $session_data['name'];
$uploaddata = null;
$config['upload_path'] = './assets/uploads/userpic';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$Img_name= strtotime(date("d-m-y H:i:s")).".jpg";
//$Img_name= strtotime(date("d-m-y H:i:s"));
$config['file_name']= $Img_name ;
$config['overwrite'] = "true";
$this->load->library('upload', $config);
if (!$this->upload->do_upload('documents')) {
$error = array('error' => $this->upload->display_errors());
} else {
$uploaddata = array('upload_data' => $this->upload->data());
}
$upload_data = $this->upload->data();
$file_name = $name;
$filepath1= $config['upload_path'];
if(!is_uploaded_file($_FILES['documents']['tmp_name'])) {
echo 'No upload';
$data = array(
'name' => $this->input->post('firstname'),
'lname' => $this->input->post('lastname')
);
}else
{
echo "file uploaded";
$data = array(
'name' => $this->input->post('firstname'),
'lname' => $this->input->post('lastname'),
'filepath'=> $Img_name,
'filename'=>$file_name
);
}
$this->load->model('update_model');
$this->update_model->update_entry($id, $data); //passing control to update_model
$session_data['name'] = $this->input->post('firstname');
$this->session->set_userdata('logged_in', $session_data); //settting sesssin values
if( $data)
{
$data['message']="Image Uploaded Successfully";
$this->load->view('endusers/endemp_edit_profile_view',$data);
}
//redirect('endusers/end_emp_home');
//}
}
This is my view
<?php echo form_open_multipart('endusers/editprofile/edit_profile');?>
<table width="auto" border="1" style="border-color: #bce8f1;"class="table table-bordered" >
<tbody>
<tr>
<td colspan="2"><input type="text" class="txt-field uname" name="firstname" placeholder="first name" required></td>
</tr>
<! ------ <tr>
<td colspan="2"><input type="text" class="txt-field pass" name="lastname" placeholder="last name" required ></td><br>
</tr>
<tr>
<tr>
<td valign="top">
<label for="curtexp" style="margin-top: 20px;padding-right: 7px;margin-left: 11px;"> Upload a Profile Picture </label> <label class="error" ></label>
<input type="file" name="documents" size="40" / style="margin-left: 9px;" placeholder="Profile Picture"><br><br>
</td>
</tr>
<tr>
<td colspan="8" style="text-align:center">
<br />
<div class="ee">
<input type="submit" class="btn btn-primary" value="Update" class="buttonmy2" / style="float: left; margin-bottom: 17px;">
<input type="Reset" class="btn btn-primary" value="Reset" class="buttonmy2" / style="float: left;margin-left: 10px;">
<?php echo validation_errors(); ?>
</div>
</td>
</tr>
</tbody>
</table>
</form>
This is my code in codeigniter for uploading a profile pic but it takes ma to log out to see the uploaded image .I want tom upload a image without logging out of my account and may be jusr refreshing the page
--This Jquery plugin will solve your problem
--More help http://malsup.com/jquery/form/#getting-started
<script src="http://malsup.github.com/jquery.form.js"></script>
$("#form_id").on('submit', (function (e) {
var options = {
url:'your URL', // target element(s) to be updated with server response
success:function(data){
alert('submitted');
}
$(this).ajaxSubmit(options);
e.preventDefault();
}));
Comment me if you need help...

codeIgniter form validation not working with file upload option

I am trying this code but it is not working with file upload.
When there is no file uploading field it works fine but with file upload its not working.
Please help me.
form.php
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('emp'); ?>
<form name="ajaxform" id="ajaxform" action="<?php echo base_url().'/emp/operation'?>" method="POST" enctype="multipart/form-data">
<table border="1">
<tr>
<th>Name</th>
<td><input type="text" name="name" value="<?php echo $name; ?>" size="50"/></td>
</tr>
<tr>
<th>Email </th>
<td><input type="text" name="email" value="<?php echo $email; ?>"/></td>
</tr>
<tr>
<th>Address</th>
<td><input type="text" name="address" value="<?php echo $address; ?>" /></td>
</tr>
<tr>
<th>Phone</th>
<td><input type="text" name="phone" value="<?php echo $phone; ?>" /></td>
</tr>
<tr>
<th>Photo</th>
<td><input type="file" name="userfile" /></td>
</tr>
</table>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="hidden" id="btn" value="<?php echo $submit; ?>" name="btn"/>
<input type="submit" id="simple-post" value="<?php echo $submit; ?>" name="simple-post"/>
</form>
</body>
Here is the controller code.
Emp.php
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name','required','callback_username_check');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[registration.email]');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('phone', 'Phone', 'required');
if (empty($_FILES['userfile']['name']))
{
$this->form_validation->set_rules('userfile', 'Photo', 'required');
}
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1800';
$config['max_width'] = '1924';
$config['max_height'] = '1924';
$new_name = time().$_FILES['userfile']['name'];
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload() OR $this->form_validation->run() == FALSE)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->emp_model->add_data();
$query['value']=$this->GetAll();
}
}
try this
if (null != ($_FILES['userfile']['tmp_name']))
{
$this->form_validation->set_rules('userfile', 'Photo', 'callback_checkPostedFiles');
}
And this function for validation
public function checkPostedFiles()
{
$file_name = trim(basename(stripslashes($_FILES['userfile']['name'])), ".\x00..\x20");
$file_name = str_replace(" ", "", $file_name);
if (isset($_FILES['userfile']) && !empty($file_name)) {
$allowedExts = array("jpeg", "jpg", "png"); // use as per your requirement
$extension = end(explode(".", $file_name));
if (in_array($extension, $allowedExts)) {
return true;
} else {
$this->form_validation->set_message('checkPostedFiles', "You can upload jpg or png image only");
return false;
}
} else {
$this->form_validation->set_message('checkPostedFiles', "You must upload an image!");
return false;
}
}
Check this sample code for image uploading :
Form :
<form method="post" action="" id="upload_file">
<div class="modal-body">
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">Profile Picture</label>
<input type="file" id="userfile" accept="image/*" name="userfile">
<p class="help-block">Requested size : 215*215</p>
</div>
</div>
</div>
<div class="modal-footer">
<div class="btn-group">
<button type="button" class="btn btn-default md-close" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" name="submit" value="Save Changes">
</div>
</div>
</form>
Include this js in your view.
Js for form submission
$(function() {
$('#upload_file').submit(function(e) {
e.preventDefault();
$.ajaxFileUpload({
url:'<?php echo base_url(); ?>profile/upload_file',
type: "POST",
fileElementId :'userfile',
success: function() {
alert("file uploaded!");
}
});
return false;
});
});
In controller
$userid = $session_data['id'];
$file_element_name = 'userfile';
$pathToUpload = '/var/www/html/uploads/' . $userid;
if ( ! file_exists($pathToUpload) )
{
$create = mkdir($pathToUpload, 0777);
if (!$create)
return;
}
$config['upload_path'] = $pathToUpload;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name))
{
$this->upload->display_errors();
}
else
{
$data = $this->upload->data();
//$this->user->insert_file($data['file_name'], $userid);
$data = array(
'userPhoto' => $data['file_name']
);
$this->db->where('user_id', $userid);
$this->db->update('tbl_user_profile', $data);
}
//#unlink($_FILES[$file_element_name]);

You did not select a file to upload, file upload error in Codeigniter

I'm trying to add singer information with image. the information can be successfully added without image but if i try to insert data with image, error occurred with "You did not select a file to upload" message.
My controller function is this...
public function add_edit_singer($id = '')
{
if($this->input->post('save_singer_info')){
$post=$this->input->post();
$add=array(
'name'=>$post['sname'],
'bio'=>$post['bio']
);
$img1=$_FILES['photo']['name'];
if($img1){
$config['upload_path'] = 'images/singer/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$error = $this->upload->display_errors();
$this->session->set_flashdata('info',$error);
redirect('admin/singer_manager');
}
$data = array('upload_data' => $this->upload->data());
$image = $data['upload_data']['file_name'];
$add['image']=$image;
}
else{
$add['image']='no_image.jpg';
}
$table='tbl_singer';
if($this->singer_manager->add($add,$table)){
$this->session->set_flashdata('info',"Artist/Band Information Added Successfully.");
redirect('admin/singer_manager');
}
else{
redirect('admin/singer_manager');
}
}
else{
$data['main_content']='admin/singer/add_edit_singer_view';
if($id != '')
$data['editdata'] = $this->common_model->get_where('tbl_singer', array('id' => $id));
$this->load->view('admin/include/template_view',$data);
}
}
my Model function is...
function add($data,$table){
$name = $data['name'];
$res = $this->db->insert($table,$data);
if($res)
{
return true;
}
else
{
return false;
}
}
and my Form is...
<form role="form" enctype="multipart/form-data" action="<?php echo base_url('admin/singer_manager/add_edit_singer/' . $id); ?>"
method="post"
id="user_form">
<table class="table table-hover">
<tr>
<td>Singer Name</td>
<td>
<input type="text" class="form-control required" name="sname"
value="<?php if (isset($name)) {
echo $name;
} ?>"/>
</td>
</tr>
<tr>
<td>Bio</td>
<td>
<textarea class="form-control required" name="bio">
<?php if (isset($bio)) {
echo $bio;
} ?>
</textarea>
</td>
</tr>
<tr>
<td>Image</td>
<td>
<?php if (isset($image)) {?>
<div class="row">
<div class="col-xs-6 col-md-4">
<a href="<?php echo base_url().'images/singer/'.$image;?>" class="thumbnail">
<img src="<?php echo base_url().'images/singer/'.$image;?>" width="120" height="140" id="img_prev" />
</a>
</div>
</div>
<?php
} ?>
<input type="file" name="photo" size="20" />
</td>
</tr>
<tr>
<td colspan="2">
<?php if (isset($id)) { ?><input type="hidden" name="id" value="<?php echo $id; ?>" /><?php } ?>
<input type="submit" class="btn btn-success" id="btn_save" name="save_singer_info"value="Save"/>
<input type="reset" class="btn btn-info" id="reset"/>
<input type="button" class="btn btn-danger" value="Cancel"onclick="history.go(-1)"/>
</td>
</tr>
</table>
</form>
change
$this->upload->do_upload()
to
$this->upload->do_upload('photo')
may be it solve the problem

Resources