Codeigniter: Submitted form is not showing values - php

Kindly have a visit on following link: Demo link for problem
You can view that form is not showing submitted values and not uploading file.
Following is form code :
<form id="frm" name="frm" method="post" enctype="multipart/form-data">
<fieldset class="fieldset2">
<legend class="legend2">Add Bid Type</legend>
<div class="form_fields">
<p>
<label for="subject" class="label2">Bid Type:</label>
<input type="input" id="type" name="type" class="textfield2" />
</p>
<p>
<label for="subject" class="label2">Bid Type Code:</label>
<input type="input" id="code" name="code" class="textfield2" />
</p>
<p>
<label for="description" class="label2">Bid Type Description:</label>
<textarea id="description" name="description" class="textarea2"></textarea>
</p>
<p>
<label for="userfile" class="label2">Icon:</label>
<input type="file" id="userfile" name="userfile" class="input_file" />
</p>
</div>
</fieldset>
<fieldset class="none">
<p>
<input type="submit" id="btnsubmit" name="btnsubmit" class="btn" value="Add" />
<input type="reset" id="btnreset" name="btnreset" class="btn" value="Reset" />
</p>
</fieldset>
</form>`
And following is controller code :
function create() {
$data = '';
echo '<pre>';
echo 'below is Post Fields Data:<br>';
print_r($this->input->post());
echo '<br>Post Fields Data Ends: ';
echo '</pre>';
$this->form_validation->set_rules('type', 'Bid Type', 'trim|required|xss_clean');
$this->form_validation->set_rules('code', 'Bid Type Code', 'trim|xss_clean');
$this->form_validation->set_rules('description', 'Bid Type description', 'trim|xss_clean');
$data['errors'] = array();
if ($this->form_validation->run()) {
$data = array(
'code' => $this->form_validation->set_value('code'),
'type' => $this->form_validation->set_value('type'),
'description' => $this->form_validation->set_value('description'),
);
if (!is_null($this->bid_types->create_bid_type($data))){
$data['errors']['success'] = 'Record Successfully Added!';
} else {
$errors = $this->bid_types->get_error_message();
foreach ($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
}
$config['upload_path'] = base_url().'resource/images/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())
{
$data['errors']['upload'] = 'Not Uploaded';
//$data['errors']['upload'] = $this->upload->display_errors();
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
}
else
{
$data['errors']['upload'] = 'Yes Uploaded';
//$data['errors']['upload'] = $this->upload->data();
//$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
echo '<pre>';
print_r($this->upload->data());
echo '</pre>';
} /* END OF FORM VALIDATRION RUN */
if(!$this->input->is_ajax_request() && !$this->input->get_post('is_ajax'))
{
$this->load->view('bend/bid_type/create', $data);
}
} /* END OF FUNCTION CREATE */
Can some one guide me what and where Iam doing wrong and how it can be rectrified.

File data appears in $_FILE not in $_POST.
I think this line is your problem:
$config['upload_path'] = base_url().'resource/images/uploads/';
This $config['upload_path'] should point to a file system path, not a URL.
Try something more like:
$config['upload_path'] = realpath(APPPATH . '../resource/images/uploads/');
which will start from your application folder, up one folder then into resource/images/uploads
change it accordingly if its somewhere else.
p.s. Also check your write permissions.

put $this->output->enable_profiler(TRUE) on the first line of create() function, it will show you very usefull information $_POST global to debug your code like

Charles shows that the request from the form submit does contain the post data.
Does anything else happen before the create() function that could wipe the POST data?

I would add an action attribute to the form using echo form_open('/phpdemo/b1/admin/bid_type/create') as described in the user manual.

you can not use
$this->form_validation->set_rules('code', 'Bid Type Code', 'trim|xss_clean');
in multipart , so you must create your own function for validation and use callbacks

Related

My Image is not upload to the specific directory in codeigniter in my Local wamp server its display directory is not correct error

I have spent days trying to make this work based on the examples in the documentation but I am missing something
I have a simple image upload form on my local server where users can upload image and it need to store in local folder but It not work . I am also take a permission to my folder is 777 but still its taking problem what I can do that. My Image is not upload to the specific directory in codeigniter in my Local wamp server its display directory is not correct error.
MY Controller Code is given below
public function do_upload()
{
$upPath= './uploads/';
if(!file_exists($upPath))
{
mkdir($upPath, 0777, true);
}
$config = array(
'upload_path' => $upPath,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile'))
{
$data['imageError'] = $this->upload->display_errors();
print_r($data['imageError']);
}
else
{
$imageDetailArray = $this->upload->data();
return $imageDetailArray['file_name'];
}
}
public function add_article(){
if($this->session->userdata('writer_logged_in')){
if ($this->form_validation->run() === FALSE)
{
$data['main_content']="writer/profile";
$this->load->view('include/template',$data);
}
else
{
$image = $this->do_upload();
echo $image;
die();
$result=$this->writer_model->new_article();
if($result==1){
$this->session->set_flashdata('article_insertion',"Article has been successfully added.");
redirect('writer/profile');
}
}
}
else{
redirect('site');
}
}
My View Code is given below
<form method="post" action="<?php echo base_url(); ?>writer/add_article" enctype="multipart/form-data">
<?php echo validation_errors(); ?>
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" placeholder="Article title" value="<?php echo set_value('title'); ?>" class="form-control" />
</div>
<div class="form-group">
<label>Image</label>
<input type="file" id="image" name="userfile" class="form-control" value="<?php echo set_value('userfile'); ?>"/>
</div>
<div class="form-group">
<label for="body">Article detail</label>
<textarea name="body" id="body" class="form-control" rows="10" cols="40" placeholder="Provide Articles content. Basic HTML is allowed."><?php echo set_value('body'); ?></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
Now My Error is Resolved I have just clear my temp caches and now my image is upload to the specific folder . Thanks For you cooperation and Answers. Now I am Continued my work.......!
I recommend this code:
$upPath = './uploads/';
if(!file_exists($upPath))
{
mkdir($upPath, 0777, true);
}
$config['upload_path'] = $upPath;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
for more details please read the CodeIgniter manual

Image Name Not Save In Database in CodeIgniter 3

Hi friends in my code image upload in my folder but it's not saved in database am not knowledge about CI so help me for this
view code for upload image,
<form role="form" method="post" enctype="multipart/form-data">
<fieldset class="form-group">
<input type="hidden" name="txt_hidden" value="" class="form-control">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput">Add Main Caregory</label>
<input type="text" value="<?php echo set_value('p_name'); ?>" placeholder="Main Category" name="p_name" class="form-control">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput2">Order</label>
<input type="text" name="order_id" placeholder="Order Id" value="<?php echo set_value('order_id'); ?>" class="form-control" id="formGroupExampleInput2">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput2">Status ( 0 active , 1 inactive)</label>
<input type="text" name="status" placeholder="Status" value="<?php echo set_value('status'); ?>" class="form-control" id="formGroupExampleInput2">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput2">Image</label>
<input type="file" name="image" placeholder="Category Image" value="<?php echo set_value('image'); ?>" class="form-control" id="formGroupExampleInput2">
</fieldset>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Category</button>
</div>
</form>
in My controller
use this for add category with an image
function add_main_category()
{
$this->form_validation->set_rules('p_name', 'Main Category', 'required|is_unique[main_category.p_name]');
$this->form_validation->set_rules('order_id', 'Order Id');
$this->form_validation->set_rules('status', 'Status');
$this->form_validation->set_rules('image', 'Category Image', 'callback_cat_image_upload');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('master/add_main_category');
}
else
{
$p_name = strtolower($this->input->post('p_name'));
$order_id = strtolower($this->input->post('order_id'));
$status = strtolower($this->input->post('status'));
$image = strtolower($this->input->post('image'));
$data = array(
'p_name' => $p_name,
'order_id' => $order_id,
'status' => $status,
'image' => $image
);
$insert = $this->m->CategoryAdd($data);
if($insert){
$this->session->set_flashdata('success_msg','Category Created Successfully!!');
}
else{
$this->session->set_flashdata('error_msg', 'Failed to delete Category');
}
redirect('admin/main_cat');
}
}
and for image upload use this in controller
function cat_image_upload(){
if($_FILES['image']['size'] != 0){
$upload_dir = './uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir);
}
$config['upload_path'] = $upload_dir;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = 'userimage_'.substr(md5(rand()),0,7);
$config['overwrite'] = false;
$config['max_size'] = '5120';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')){
$this->form_validation->set_message('cat_image_upload', $this->upload->display_errors());
return false;
}
else{
$this->upload_data['file'] = $this->upload->data();
return true;
}
}
else{
$this->form_validation->set_message('cat_image_upload', "No file selected");
return false;
}
}
table main_category in column image
everything is working only no image name save in database!
help me with this.
Thanks in advance!
You can't store the image in the way you used as:
$image = strtolower($this->input->post('image'));
Get the image file name once it is uploaded. Here is your modified code:
if (!$this->upload->do_upload('image')){
$this->form_validation->set_message('cat_image_upload', $this->upload->display_errors());
return false;
}
else{
$this->upload_data['file'] = $this->upload->data();
$data = array('upload_data' => $this->upload->data());
$doc_file_name = $data['upload_data']['file_name']; // Use this file name to store in database
return true;
}

Two form on the same view in CodeIgniter

I have a problem in Codeigniter when i have two upload forms on the same view/site. It seems Codeigniter recognizes only submit from one form. Even if i change the input name in the form, problem still exists.
Below is my view and controler:
View:
<?php echo form_open_multipart('admin/upload/do_upload_ac');?>
<input type="file" name="actives" />
<br /><br />
<input class="btn btn-primary" type="submit" value="Add Active Emails" />
</form>
<?php echo form_open_multipart('admin/upload/do_upload_un');?>
<input type="file" name="unactives" />
<br /><br />
<input class="btn btn-primary" type="submit" value="Add Active Emails" />
</form>
And the controler:
public function do_upload_ac()
{
header('Cache-Control: must-revalidate');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = 0;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('actives'))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', 'Csv Data Import failed!! Please check the file size or file type!');
//$this->load->view('admin/muc', $error);
redirect(base_url() . 'index.php/admin/muc');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename_ac = $this->upload->data('full_path');
$this->Upload_model->insert_csv_ac($filename_ac);
$this->Upload_model->clean_temp_ac();
$this->Upload_model->truncate_temp_ac();
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully!');
//$this->load->view('success', $data);
//unlink($filename_ac);
redirect(base_url() . 'index.php/admin/muc');
}
}
public function do_upload_un()
{
header('Cache-Control: must-revalidate');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('unactives'))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', 'Csv Data Import failed!! Please check the file size or file type!');
//$this->load->view('admin/muc', $error);
redirect(base_url() . 'index.php/admin/muc');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename_ac = $this->upload->data('full_path');
$this->Upload_model->insert_csv_un($filename_un);
$this->Upload_model->clean_temp_un();
$this->Upload_model->truncate_temp_un();
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully!');
//$this->load->view('success', $data);
unlink($filename_un);
redirect(base_url() . 'index.php/admin/muc');
}
}
I was searching here for similar problem, but i just found uploading multiple files at once. Any ideas? And thank you all.
Alright, to give you an understanding of multiple submit, I have rendered a sample view for you. I have created following view
With this code
<div class="container">
<div class="col-md-3" style="box-shadow: 2px 2px 2px grey">
<form action="<?php echo base_url().'admin/upload/do_upload_ac'?>" method="post" id="form1">
<div class="form-group">
<label > Upload 1</label>
<input type="file" name="file1" >
</div>
<div class="form-group">
<button type="button" class="btn btn-danger" onclick="submitOne()">Upload Image1</button>
</div>
</form>
</div>
<div class="col-md-3" style="box-shadow: 2px 2px 2px grey">
<form action="<?php echo base_url().'admin/upload/do_upload_un'?>" method="post" id="form2">
<div class="form-group">
<label > Upload 2</label>
<input type="file" name="file1" >
</div>
<div class="form-group">
<button type="button" class="btn btn-danger" onclick="submitTwo()">Upload Image2</button>
</div>
</form>
</div>
</div>
And this Script
<script>
function submitOne()
{
$('#form1').submit();
}
function submitTwo()
{
$('#form2').submit();
}
</script>
I press first button I get
Similarly If i press second button it shows me form2 will be submitted. What you do once the form is submitted is all in the URL where you are submitting the form.
Your code is working with no update required. But you should change html form close with codeigniter's form_close() or user html form tag to start form.

Codeigniter: trying to update profile with codeigniter

I'm new to codeigniter, and I'm trying to make a form that updates a profile. It has worked before but now it doesn't anymore.
I changed something in the view and it stopped working. I changed it back to where it was but it doesn't work anymore. I spend half a day trying to make it work again but I failed.
Maybe I don't see what you guys can see. At least I hope so.
this is my view:
<?php echo form_open_multipart('Gids/do_upload');?>
<img width="200px" src="<?php echo base_url()."uploads/".$profile[0]['image']; ?>" alt=""/>
<label for="">Uploade new picture:</label><input type="file" name="userfile" size="20" value="128.jpg" >
<input style="display: none" id="image" name="image" type="text" value="<?php echo "profile_picture".$_SESSION['id'].".jpg"; ?>"/>
<label for="naam">Naam:</label><input id="naam" name="naam" type="text" value="<?php echo $profile[0]['naam']; ?>"/>
<label for="voornaam">Voornaam:</label><input id="voornaam" name="voornaam" type="text" value="<?php echo $profile[0]['voornaam']; ?>"/>
<label for="email">Gebruikersnaam:</label><input id="gebruikersnaam" name="gebruikersnaam" type="text" value="<?php echo $profile[0]['gebruikersnaam']; ?>"/>
<label for="email">Email:</label><input id="email" name="email" type="text" value="<?php echo $profile[0]['email']; ?>"/>
<label for="opleiding">Opleiding:</label><input id="opleiding" name="opleiding" type="text" value="<?php echo $profile[0]['opleiding']; ?>"/>
<label for="school">School:</label><input id="school" name="school" type="text" value="<?php echo $profile[0]['school']; ?>"/>
<label for="wachtwoord">Wachtwoord:</label><input id="wachtwoord" name="wachtwoord" type="text" />
<label for="typeAgain">Type Opnieuw:</label><input id="typeAgain" type="text" />
<label for="over">Over mezelf:</label><textarea name="over" id="over" cols="30" rows="10"><?php echo $profile[0]['over']?></textarea>
<input type='text' style="display: none" name='student_id' value="<?php echo $profile[0]['student_id']?>"/>
<button class="btn btn-default" id="changeprofile" type="submit">Wijzigingen opslaan</button>
</form>
As you can see there is also a image upload
this is my controller:
function do_upload()
{
$this->load->model("Gids_model",'',true);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['file_name'] = 'profile_picture'.$_SESSION['id'].'.jpg';
$config['overwrite'] = 'TRUE';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error_upload = array('error' => $this->upload->display_errors());
redirect('Gids/datum', $error_upload);
}
else
{
$this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
//$e = $this->input->post('student_id');
$data_upload = array('upload_data' => $this->upload->data());
redirect('Gids/datum', $data_upload);
}
}
And this is my model:
public function update_profile($id, $voornaam, $naam, $email, $wachtwoord, $opleiding, $school, $over, $image, $gebruikersnaam){
$data = array(
'student_id' => $id,
'voornaam' => $voornaam,
'naam' => $naam,
'email' => $email,
'wachtwoord' => $wachtwoord,
'opleiding' => $opleiding,
'school' => $school,
'over' => $over,
'image' => $image,
'gebruikersnaam' => $gebruikersnaam
);
$this->db->where('student_id', $id);
$this->db->update('tbl_student', $data);
}
Could you please help me out.
Also I don't know what the form_open_multipart('Gids/do_upload') is for, I got it from a tutorial to upload images with codeigniter.
Here is what i tried
public function do_upload() {
$this->load->model("Gids_model",'',true);
// load library or you can do it with autoloading
// feature of CI
$this->load->library('upload');
// make sure that the folder uploads is created
// at the root directory of the project
$config = array(
'upload_path' => './uploads',
'allowed_types' => 'jpg|jpeg|JPG|JPEG|png',
'max_size' => '1000',
'file_name' => 'profile_picture.jpg',
'overwrite' => true
);
// use initialize instead
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error_upload = array('error' => $this->upload->display_errors());
// you can check the errors here by using var_dump();
// just uncomment the the line below
// var_dump($this->upload->display_errors());die();
redirect('Gids/datum', $error_upload);
}
else
{
$this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
//$e = $this->input->post('student_id');
$data_upload = array('upload_data' => $this->upload->data());
redirect('Gids/datum', $data_upload);
}
}
I tried this and hope it helps:
Controller :
public function upload_profile() {
$input = $this->input->post();
$config['upload_path'] = './uploads/profile_pics/'; //path were I save the uploaded profile pics
$config['allowed_types'] = 'gif|jpg|png'; // allowed types that is mention
//size of the picture by default
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = true;
$this->load->library('upload', $config);
// display error if the picture is not on the config (sample bmp)
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors(); // display the errors
$data['upload_error'] = $error;
if($this->session->userdata('account_id') != null) { // if there is an account
$this->load->model('profile'); //model
$this->load->model('account'); //model
$data['user'] = $this->profile->get_profile($this->session->userdata('account_id')); //get_profile is a function in model
$data['account'] = $this->account->get_account($this->session->userdata('account_id')); //get_account is a function in model
$data['view'] = 'users/settings';
$data['title'] = "User's Settings";
$data['error'] = $error;
$this->load->view('masterpage', $data);
} else {
redirect(base_url('index.php/qablog/login'));
}
}
else
{
//if no error
$data = $this->upload->data();
$updateProfile = array(
'profile_pic' => $data['file_name']
);
$this->load->model('profile');
$this->profile->update_profile($this->session->userdata('account_id'), $updateProfile); // update the profile of the user
redirect(base_url('index.php/users/profile'));
}
}
Model get_profile():
public function get_profile($profile_id)
{
$this->db->select()->from('profile_tbl')->where('profile_id', $profile_id);
$query = $this->db->get();
return $query->first_row('array');
}
Model update_profile():
public function update_profile($profile_id, $data)
{
$this->db->where('profile_id', $profile_id);
$this->db->update('profile_tbl', $data);
return $this->db->affected_rows();
}
Model get_account():
public function get_account($account_id)
{
$this->db->select()->from('account_tbl');
$this->db->where('account_id', $account_id);
$query = $this->db->get();
return $query->result_array();
}
View :
//if there is an error
<?php
if ($error == 3) {?>
<div class="alert alert-success">
×
<strong>Success!</strong> Account or Profile Changed Successfully.
</div>
<?php } else if ($error == 1) { ?>
<div class="alert alert-warning">
×
<strong>Warning!</strong>Password Entered is Incorrect!.
</div>
<?php }else if ($error == 2) { ?>
<div class="alert alert-warning">
×
<strong>Warning!</strong>New Password and Confirm Password!.
</div>
<?php }?>
// default.png if haven't uploaded profile picture
<?php $profilePic = "default.png"; ?>
// if already uploaded profile picture it will display
<?php if($user['profile_pic'] != null) { ?>
<?php $profilePic = $user['profile_pic']; ?>
<?php } ?>
// if there is an error in uploading
<?php if(isset($upload_error)) { ?>
<div class="col-lg-12">
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Uploading profile image could not be completed. <?php print_r($upload_error); ?>
</div>
</div>
<?php } ?>
<div class="col-lg-12">
//display the profile picture
<img src="<?php echo base_url('uploads/profile_pics/'.$profilePic); ?>" width="100" />
// call the controller upload_profile
<?php echo form_open_multipart(base_url('index.php/users/upload_profile'));?>
<input type="file" name="userfile" id="userfile" size="20" style="display:none;" />
<label for="userfile" class="btn btn-info btn-sm">Choose Image</label>
<input type="submit" class="btn btn-xs" value="edit profile" />
</form>
<h3 class="text-info"><?php echo $user['fname'].' '.$user['lname']; ?></h3>
</div>
Just follow this, and if you have problem just tell me. :)

How to save image in folder using CodeIgniter?

I am new to CodeIgniter. I want to know how to save an image in a folder. But I wrote the code like image name was stored in a table. But I want to store the image in a folder and retrieve image from the folder. Here I am using the code to store image name in table:
In Controller:
public function product()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('productname','Product Code','trim|required');
$this->form_validation->set_rules('productcode','Product Code','trim|required');
$this->form_validation->set_rules('productprice','Product Price','trim|required');
$this->form_validation->set_rules('quantity','Quantity','trim|required');
$this->form_validation->set_rules('uploadimage','Upload Image','trim_rquired');
if($this->form_validation->run()==FALSE)
{
$this->index();
}else
{
$data['query']=$this->main_model->product_db();
$this->load->view('query_view',$data);
}
}
In Model:
public function product_db()
{
$data=array(
'productname'=>$this->input->post('productname'),
'productcode'=>$this->input->post('productcode'),
'productprice'=>$this->input->post('productprice'),
'quantity'=>$this->input->post('quantity'),
'image'=>$this->input->post('uploadimage')
);
$query=$this->db->get("product");
if($query->num_rows())
{
$this->db->insert('product',$data);
$query=$this->db->get("product");
$this->session->set_userdata($data);
return $query->result();
}
return false;
}
In View:(form page)
<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open("main/product"); ?>
<p>
<label for="product_name">Product Name:</label>
<input type="text" id="productname" name="productname" value="<?php echo set_value('product_name'); ?>" />
</p>
<p>
<label for="ProductCode">Product Code</label>
<input type="text" id="productcode" name="productcode" value="<?php echo set_value('productcode'); ?>" />
</p>
<p>
<label for="productprice">Product Price:</label>
<input type="text" id="productprice" name="productprice" value="<?php echo set_value('productprice'); ?>" />
</p>
<p>
<label for="Quantity">Quantity:</label>
<select name="quantity" id="quantity" value="<?php echo set_value('quantity'); ?>" /><option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</p>
<p>
<label for="Uploadimage">Upload Image:</label>
<input type="file" name="uploadimage" id="uploadimage" value="<?php echo set_value('quantity'); ?>" />
</p>
<p>
<input type="submit" class="greenButton" value="submit" />
</p>
<?php echo form_close();
?>
In View(query_view Page):
<?php
echo "<table border='2'>
<tr>
<th>productid</th><th>productname</th><th>productcode</th><th>productprice</th>
<th>quantity</th><th>image</th>
</tr>";
foreach($query as $r)
{
echo "<tr>";
echo "<td>".$r->productid."</td>"."<td>".$r->productname."</td>".
<td>".$r>productcode."</td>"."<td>".$r->productprice."</td>"."<td>"
.$r->quantity."</td>"." <td>".$r->image."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
?>
This is just a sample code of uploading an image:
<?php
$configUpload['upload_path'] = './uploads/'; #the folder placed in the root of project
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; #allowed types description
$configUpload['max_size'] = '0'; #max size
$configUpload['max_width'] = '0'; #max width
$configUpload['max_height'] = '0'; #max height
$configUpload['encrypt_name'] = true; #encrypt name of the uploaded file
$this->load->library('upload', $configUpload); #init the upload class
if(!$this->upload->do_upload('uploadimage')){
$uploadedDetails = $this->upload->display_errors();
}else{
$uploadedDetails = $this->upload->data();
}
print_r($uploadedDetails);die;
?>
you are seeking this kind of code
$config['upload_path'] = './files'; //core folder (if you like upload to application folder use APPPATH)
$config['allowed_types'] = 'gif|jpg|png'; //allowed MIME types
$config['encrypt_name'] = TRUE; //creates uniuque filename this is mainly for security reason
$this->load->library('upload', $config);
if (!$this->upload->do_upload('picture_upload')) { //picture_upload is upload field name set in HTML eg. name="upload_field"
$error = array('error' => $this->upload->display_errors());
}else{
//print_r($this->upload->data()); // this is array of uploaded file consist of filename, filepath etc. print it out
$this->upload->data()['file_name']; // this is how you get for example "file name" of file
}
Please follow this guide http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html it has everything in it. If you need help with logic its pretty simple
form -> upload field -> button -> form sent -> check rules if file is OK -> upload file -> save data (filename, filepath...) in table.

Categories