I am new to Codeigniter and new to the MVC webdesign, i have plant insert form i want to upload image and save image name into database i tried many codes but not works please help me to write upload function
plant insert form is below here
<?php echo validation_errors(); ?>
<?php echo form_open('AddPlant/InsertPlant')?>
<div class="form-group has-error">
<label for="name">Name <span class="require">*</span></label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea rows="5" class="form-control" name="description" >
</textarea>
</div>
<div class="form-group required">
<label for="exampleSelect1" class='control-label'>Job Type</label>
<select class="form-control" id="age" name="age">
<option value="1">Level 1</option>
<option value="2">Level 2</option>
<option value="3">Level 3</option>
</select>
</div>
<div class="form-group">
<p><span class="require">*</span> - required fields</p>
</div>
<div class="form-group">
<label for="description">Plant Image</label>
<input type="file" name="plantimg">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">
Create
</button>
<button class="btn btn-default">
Cancel
</button>
</div>
<?php echo form_close();?>
My Codeigniter model
<?php
class Model_plants extends CI_Model
{
function insertPlantData(){
$data =array(
'name'=> $this->input->post('name',TRUE),
'description'=> $this->input->post('description',TRUE),
'age'=> $this->input->post('age',TRUE),
);
return $this->db->insert('plants',$data);
}
}
Hope this will help you :
Note : make sure you have load database and upload library either in autoload or in controller
First your form should be like this :
<?php echo form_open_multipart('AddPlant/InsertPlant')?>
............
<?php echo form_close();?>
Your controller should be like this :
public function InsertPlant()
{
/*here make sure your path is correct*/
$config['upload_path'] = FCPATH .'assets/uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('plantimg'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
else
{
//$data = array('upload_data' => $this->upload->data());
$file_name = $this->upload->data('file_name');
/*here assuming that your column name for image is image_name, change it not*/
$data =array(
'name'=> $this->input->post('name',TRUE),
'description'=> $this->input->post('description',TRUE),
'age'=> $this->input->post('age',TRUE),
'image_name'=> $file_name,
);
return $this->db->insert('plants',$data);
}
}
Related
I am trying to build a project with product information with image. I have tried some code given below, but I can not upload image path in the database. I am a newbie trying to learn code.
controller code:-
class Category extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Category_model');
}
private function do_upload(){
$config['upload_path']='./uploads/';
$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('product_image')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/pages/add_product_form', $error);
}
else
{
$upload_data = $this->upload->data();
$this->Category_model->insert_data( $upload_data['upload_path']);
}
$data = array('upload_data' => $this->upload->data());
}
function save_product(){
$this->do_upload();
$this->session->set_userdata('message','Product saved successfully');
$this->add_product();
}
}
Model code:-
function insert_data( $path_name){
$data = array(
'product_image' => $path_name
);
$data['product_status']=1;
$this->db->insert('tbl_product', $data);
}
view code:-
<form class="form-horizontal" action="<?php echo base_url()?>index.php/Category/save_product" enctype="multipart/form-data" method="post">
<fieldset>
<div class="control-group">
<label class="control-label" for="typeahead">Product Name </label>
<div class="controls">
<input type="text" name="product_name" class="span6 typeahead" id="typeahead" data-provide="typeahead" data-items="4" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Product Category</label>
<div class="controls">
<select name="product_category" id="selectError3">
<option>Select Category</option>
<?php foreach($category_info as $category){ ?>
<option value="<?php echo $category->category_id ?>"><?php echo $category->category_name; ?></option>
<?php } ?>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="typeahead">Product Image </label>
<div class="controls">
<input name="product_image" type="file" name="fileToUpload" id="fileToUpload">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
This is my code given above, but still not getting the image path in database, please help in this regard.Thanks in Advance.
Just increased max_size,max_width,max_height and these solved the problem.
myiamge
I have 2 forms as tab in signup page when one form is submit and if have any error I want to redirect to that specific tab which have error but did not happen.
Here is my view
<?php include 'header.php'; ?>
<section class="services">
<div class="container">
<div class="signup-wrapper well" style="margin-bottom: 80px; margin-top: 80px;">
<div class="container">
<div class="services__main">
<h2 class="title title--main"><span class="title__bold">Sign Up</span><span class="line line--title"><span class="line__first"></span><span class="line__second"></span></span></h2>
</div>
<div class="aside-tabs__links">
Genrel User
Dealership
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc" >
<div class="col-md-5">
<form action="<?= base_url();?>Home/add_user" method="post" >
<!-- class="quick-form" -->
<div class="form-group">
<input type="text" class="form-control" name="fname" placeholder="Full Name" />
</div>
<?php echo form_error('fname'); ?>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" />
</div>
<?php echo form_error('email'); ?>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" />
</div>
<?php echo form_error('password'); ?>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone #" />
</div>
<?php echo form_error('phone'); ?>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<?php echo form_error('city'); ?>
<div class="form-group">
<input type="submit" class="btn button button--red button--main pull-right" style="margin-bottom: 10px;" value="Sign Up">
</div>
</form>
</div>
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev" style="display: none;">
<div class="col-xs-5">
<form action="<?= base_url();?>Home/add_dealer" method="post" enctype="multipart/form-data">
<h4>Comapny Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="cname" placeholder="Company Name" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="owner" placeholder="Owner Name" />
</div>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Office Location" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Office Phone" />
</div>
<div class="form-group">
<select name="business-type" class="form-control">
<option class="form-group">Products Deal</option>
<option class="form-group">Bikes</option>
<option class="form-group">Accessories</option>
<option class="form-group">Both</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="link" placeholder="social-link(optional)" />
</div>
<div class="form-group">
<button type="button" class="button button--custom--grey button--main btn" id="logo">Upload Logo</button>
<input type="file" class="form-control hidden" id="logo1" name="logo1" /><span id="mylogo">* Format must be jpg,jpeg or png</span>
</div>
<?php echo form_error('logo'); ?>
<div class="form-group">
<textarea class="form-control" name="descrp" placeholder="Description(optional)"></textarea>
</div>
<h4>Primary Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="password" placeholder="Password" />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn button button--red button--main pull-right" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
here is my controller
function add_dealer()
{
//echo "<pre>"; print_r($_FILES);
$config['upload_path'] = 'C:\xampp\htdocs\devilbirds\images\uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = 'TRUE';
$config['overwrite'] = 'FALSE';
$config['wm_text'] = 'Deveil Birds';
$config['wm_type'] = 'text';
$config['wm_font_path'] = 'C:\xampp\htdocs\devilbirds/fonts/fontawesome-webfont.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'aabbcc';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->load->library('upload', $config);
$this->image_lib->watermark();
$abc = $this->upload->do_upload('logo1');
$upload_data = $this->upload->data();
// var_dump($abc);exit();
$file_name = $upload_data['file_name'];
$this->form_validation->set_rules('cname','CName','trim|required');
$this->form_validation->set_rules('logo1','Logo1','trim');
$this->form_validation->set_rules('address','Address','trim');
$this->form_validation->set_rules('phone','Phone','trim|is_unique[bd_dealer.phone]');
$this->form_validation->set_rules('owner','Owner','trim');
$this->form_validation->set_rules('descrp','Descrp','trim');
$this->form_validation->set_rules('business-type','Business-type','trim');
$this->form_validation->set_rules('link','Link','trim');
$this->form_validation->set_rules('email','Email','trim|is_unique[bd_dealer.email]');
$this->form_validation->set_rules('password','Password','trim|min_length[5]|max_length[20]');
$this->form_validation->set_rules('city','City','trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = $this->session->set_flashdata('errors');
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url
}
else
{
$userData = array(
'company' => $this->input->post('cname'),
'logo' => $file_name,
'location' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'name' => $this->input->post('owner'),
'description' => $this->input->post('descrp'),
'business_type' => $this->input->post('business-type'),
'social_link' => $this->input->post('link'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'city' => $this->input->post('city'));
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'user_type' => '2');
//var_dump($userData);exit();
$this->Home_m->add_dealer($userData);
$this->Home_m->users($data);
redirect('Home/login');
}
}
I have two functions for two different forms in tabs. let suppose if i post my dealer form and this form have any issue then it will redirect to the signup page and it shows the pre-active tab which is general user signup. i want to redirect to the div that contain the form or dealer signup. any help will be appreciated , thanks in advance
In your controller when you get error do this.
for form one error
$data['error'] = $this->session->set_flashdata('errors');
$data['div1'] = "div1";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
For form two error
$data['error'] = $this->session->set_flashdata('errors');
$data['div2'] = "div2";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
In your view page
<script>
$(document).ready(function(){
var div1 = "<?php $div1; ?>";
var div2 = "<?php $div2; ?>";
if(div1=='' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='div1' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='' && div2=='div2'){
$("#desc").hide();
$("#rev").show();
}
})
</script>
div1
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc">
Div2
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev">
I hope it will help you.
I have 2 websites in 1 htdocs directory. The structure's like this
The main directory it's structured like this(name:Quartiere):
Quartiere
_admin
application
assets
Second website directory it's _admin with the same structure(default CodeIgniter structure).
I'm trying to upload a file from _admin to the main directory assets folder, but for some reason, it's not working.I tried other answers from other post but nothing it helped.
What I tried:
Put 'userfile' in do_upload
Checked if the form it's multipart(it is)
What I checked:
If the path it's good(it is)
If the size of the photo it's smaller than 2048(it is)
_admin Controller:
public function add_product(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Adauga Produs';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('name','Nume','required');
$this->form_validation->set_rules('category','Categorie','required');
$this->form_validation->set_rules('ingredients','Ingrediente');
$this->form_validation->set_rules('price','Pret','required');
$this->form_validation->set_rules('grams','Gramaj','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('products/adauga', $data);
$this->load->view('templates/footer');
}else{
//upload image
$config['upload_path'] = 'http://localhost/quartiere/assets/images';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error' => $this->upload->display_errors());
$food_image = 'noimage.png';
}else{
$data = array('upload_data' => $this->upload->data());
$food_image = $_FILES['userfile']['name'];
}
$this->food_model->add_product($food_image);
redirect('produse/adauga');
}
}
_admin View:
<?php echo validation_errors();?>
<?php echo form_open_multipart('products/add_product');?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1 class="text-center">
<?php echo $title; ?>
</h1>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Nume Produs" required/>
</div>
<div class="form-group">
<select class="form-control" name="category" required>
<?php foreach($categories as $category):?>
<option value="<?php echo $category['id'];?>"><?php echo $category['category_name'];?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Bar:</label><br>
Da <input type="radio" name="bar" class="from-control" value="1"/>
Nu <input type="radio" name="bar" class="from-control" value="0"/>
</div>
<div class="form-group">
<input type="text" name="ingredients" class="form-control" placeholder="Ingrediente"/>
</div>
<div class="form-group">
<input type="number" name="grams" class="form-control" placeholder="Gramaj"/>
</div>
<div class="form-group">
<label>Poza:</label>
<input type="file" name="userfile" size="10"/>
</div>
<div class="form-group">
<input type="number" name="price" class="form-control" placeholder="Pret" required/>
</div>
<button type="submit" class="btn btn-success btn-block">Adauga Produs</button>
</div>
</div>
<?php form_close(); ?>
You can make the upload path to - '../assets/images' - or - $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images'. Either way is fine.
i am using codeigniter mvc framework, i want to make a form that has a upload picture and also save inputted data in input types in one html <form> why is this my image cannot be uploaded and it displays error like this when i echo the error:
File cannot be uploadedarray(1) { ["error"]=> string(43) "
You did not select a file to upload.
" }
Here is my code in view :
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct">
<div style="position:relative;">
<a class='btn btn-primary' href='javascript:;'>
Choose Picture..
<input type="file" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40" onchange='$("#upload-file-info").html($(this).val());'>
</a>
<span class='label label-info' id="upload-file-info"></span>
</div>
<br>
<div class="form-group">
<label for="cat_name">Product Name: </label>
<input type="text" class="form-control" name="prod_name">
</div>
<div class="form-group">
<label for="cat_desc">Description: </label>
<input type="text" class="form-control" name="prod_desc">
</div>
<div class="form-group">
<label for="cat_desc">Price: </label>
<input type="text" class="form-control" name="prod_price">
</div>
<div class="form-group">
<label for="cat_desc">Category: </label>
<select class="form-control" name="prod_cat">
<?php foreach($data as $each){ ?>
<option value="<?php echo $each->Category_id; ?>"><?php echo $each->Category_name; ?></option>';
<?php } ?>
</select>
</div>
</form>
Here is my javascript to submit the form:
function submitProduct() {
document.getElementById("frm_product").submit();
}
In my controller here is my code:
public function addProduct(){
$config['upload_path'] = './assets/uploaded_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024 * 8;
$this->load->library('upload', $config);
if($this->upload->do_upload("file_source")) {
$upload_data = $this->upload->data();
$file_name = base_url().'assets/uploaded_images/'.$upload_data['file_name'];
$data = array (
'Product_name' => $this->input->post('prod_name'),
'Product_desc' => $this->input->post('prod_desc'),
'Product_price' => $this->input->post('prod_price'),
'Category_id' => $this->input->post('prod_cat'),
'Product_img' => $file_name
);
var_dump($data);
}
else {
echo "File cannot be uploaded";
$error = array('error' => $this->upload->display_errors()); var_dump($error);
}
}
Seems to me that you forgot to add enctype="multipart/form-data" to your form
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct" enctype="multipart/form-data">
This is my code, my photo can't be inserted into the database.
Actually I want to make an online exam with the codeigniter. I want to upload the question with the pic. but when I tried to upload the pict, the code is not working.
but the question success inserted into the database. only the pict failed to upload
Controller:
function insert(){
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$this->load->library('upload', $config);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$id_soal = '';
$soal = $_POST['soal'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$kunci = $_POST['kunci'];
$status = $_POST['status'];
$data = array(
'id_soal' => $id_soal,
'soal' => $soal,
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
'kunci' => $kunci,
'status' => $status,
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
Model:
class Soal_model extends Ci_Model {
public function Ambil($where= "") {
$data = $this->db->query('select * from soal '.$where);
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
View:
<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
<div class = "row">
<div class = "col-sm-offset-3 col-sm-6">
<div class="form-group">
<label>Soal :</label>
<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
</div>
<div class="form-group">
<label>Jawaban A :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="a" id="a" required/>
</div>
<div class="form-group">
<label>Jawaban B :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="b" id="b" required/>
</div>
<div class="form-group">
<label>Jawaban C :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="c" id="c" required/>
</div>
<div class="form-group">
<label>Jawaban D :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="d" id="d" required/>
</div>
<div class="form-group">
<label>Kunci :</label>
<select name="kunci" id="kunci" class="form-control">
<option>Select</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</div>
<div class="form-group">
<label>Status :</label>
<select name="status" id="status" class="form-control">
<option value="">Select</option>
<option value="tampil">Tampil</option>
<option value="tidak">Tidak</option>
</select>
<div class="form-group">
<label>Photo :</label>
<input type="file" name="foto" id="foto" size="20"/>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-default">Simpan</button>
<button type="reset" class="btn btn-default">Hapus</button>
</div>
</div>
</div>
</form>
Database:
Kindly use this code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('soal_model');
}
function insert()
{
$config =array(
'upload_path' => './images',
'allowed_types' => 'gif|jpg|png|jpeg',
'max_size' => '2500',
);
$this->load->library('upload', $config);
$this->upload->do_upload('file_upload');
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$data = array(
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
?>