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);
}
}
?>
Related
Been trying out codeigniter and wanted to create a form with file upload using the upload class. However when i try to save the form, it didn't respond and just show the same page. I've been looking around, but I can't seem to find out how to make this work.
This is the controller Product.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Product extends CI_Controller{
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('product_m');
}
public function index(){
$data['row'] = $this->product_m->get();
$this->template->load('template', 'product/data/product_data', $data);
}
public function add(){
$product = new stdClass();
$product->product_id = null;
$product->product_name = null;
$product->customer_name = null;
$product->supplier_name = null;
$product->weight = null;
$product->product_date = null;
$product->expired_date = null;
$product->image = null;
$product->barcode = null;
$data = array(
'page' => 'add',
'row' => $product
);
$this->template->load('template', 'product/data/product_form', $data);
}
public function edit($id){
$query = $this->product_m->get($id);
if($query->num_rows()>0){
$product = $query->row();
$data = array(
'page' => 'edit',
'row' => $product
);
$this->template->load('template', 'product/data/product_form', $data);
}else{
echo "<script>alert('Data not found');";
echo "window.location='".site_url('product')."';</script>";
}
}
public function process(){
$post = $this->input->post(null, TRUE);
if(isset($_POST['add'])){
$config['upload_path'] = './uploads/product';
$config['allowed_types'] = 'jpeg|pdf|jpg|png|doc|docs|xls|xlsx';
$config['max_size'] = 5120;
$config['file_name'] = 'product-'.date('ymd').'-'.substr(md5(rand()),0,10);
$this->load->library('upload', $config);
if(#$_FILES['image']['name'] !=null){
if($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->product_m->add($post);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('success', 'Data saved successfully');
}
redirect('product');
}else{
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect('product/add');
}
}
else{
$post['image'] = null;
$this->product_m->add($post);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('success', 'Data saved successfully');
}
redirect('product');
}
}else if(isset($_POST['edit'])){
$this->product_m->edit($post);
}
}
public function del($id){
$this->product_m->del($id);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('Success', 'Data successfully deleted');
}
redirect('product');
}
}
Script for the form product_form.php
<section class="content-header">
<h1>
Product
<small>Add product</small>
</h1>
</section>
<section class ="content">
<div class="box">
<div class="box-header">
<h3 class="box-title"> product</h3>
<div class="pull-right">
<a href="<?=site_url('product')?>" class="btn btn-warning btn-flat">
<i class="fa fa-undo"></i> Back
</a>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php echo form_open_multipart('product/process');?>
<div class="form-group">
<label>Product Name *</label>
<input type="hidden" name="id" value="<?=$row->product_id?>">
<input type="text" name="product_name" value="<?=$row->product_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Customer Name *</label>
<input type="text" name="customer_name" value="<?=$row->customer_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Supplier Name *</label>
<input type="text" name="supplier_name" value="<?=$row->supplier_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Weight(kg) *</label>
<input type="number" name="weight" value="<?=$row->weight?>" class="form-control" required>
</div>
<div class="form-group">
<label>Product Date(dd/mm/yyyy) *</label>
<input type="text" name="product_date" value="<?=$row->product_date?>" class="form-control" required>
</div>
<div class="form-group">
<label>Expired Date(dd/mm/yyyy) *</label>
<input type="text" name="expired_date" value="<?=$row->expired_date?>" class="form-control" required>
</div>
<div class="form-group">
<label>Technical Data</label>
<input type="file" name="image" class="form-control" >
</div>
<!-- FORM BARCODE -->
<div class="form-group">
<label>Barcode</label>
<input name="barcode" class="form-control" value="
<?php
$angka = rand(1000000,9999999);
$random = rand(0,25);
$random1 = rand(0,25);
$random2 = rand(0,25);
$name = array("A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");
echo $name[$random],$name[$random1],$name[$random2],$angka;
$row->barcode
?>" readonly>
</div>
<div class="form-group">
<button type="submit" name="<?=$page?>" class="btn btn-success btn-flat">
<i class="fa fa-paper-plane"> Save</i>
</button>
<button type="Reset" class="btn btn-flat"> Reset</button>
</div>
<!-- </form> -->
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</section>
Load the form helper.
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('product_m');
$this->load->helper('form');
}
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 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);
}
}
I made a user registration form on the CodeIgniter framework so users can registrate to my website. Now the only thing that doesn't work is that I cant upload a profile picture. When I click on the register button I'm getting 2 errors. I want the profile picture to be uploaded in the product_foto column.
This is my view file: (register.php) :
<form action="" method="POST" enctype='multipart/form-data'>
<div class="form-group">
<label for="voornaam" >Voornaam</label>
<input class="form-control" name="voornaam" id="voornaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Achternaam</label>
<input class="form-control" name="achternaam" id="achternaam" type="text">
</div>
<div class="form-group">
<label for="achternaam">Straat en huisnummer</label>
<input class="form-control" name="straat" id="straat" type="text">
</div>
<div class="form-group">
<input class="form-control" name="huisnummer" id="huisnummer" type="text">
</div>
<div class="form-group">
<label for="huisnummer">Huisnummer</label>
<input class="form-control" name="huisnummer" id="huisnummer">
</div>
<div class="form-group">
<label for="postcode" >Postcode</label>
<input class="form-control" name="postcode" id="postcode">
</div>
<div class="form-group">
<label for="woonplaats" >Woonplaats</label>
<input class="form-control" name="woonplaats" id="woonplaats">
</div>
<div class="form-group">
<label for="email" >Email adres</label>
<input class="form-control" name="email" id="email" type="emai">
</div>
<div class="form-group">
<label for="wachtwoord" >Wachtwoord</label>
<input class="form-control" name="wachtwoord" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="wachtwoord">Herhaal wachtwoord</label>
<input class="form-control" name="wachtwoord2" id="wachtwoord" type="password">
</div>
<div class="form-group">
<label for="profiel_foto">Profiel foto</label>
<input class="form-control" type="file" name="profiel_foto" id="profiel_foto">
</div>
<div class="form-group">
<label for="beschrijving">Beschrijving</label>
<input class="form-control" name="beschrijving" id="beschrijving">
</div>
<div class="form-group">
<label for="geboortedatum" >Geboortedatum</label>
<input class="form-control" name="geboortedatum" id="geboortedatum" type="date">
</div>
<div class="form-group">
<label for="geslacht" >Geslacht</label>
<select class="form-control" id="geslacht" name="geslacht">
<option value="Man">Man</option>
<option value="Vrouw">Vrouw</option>
</select>
</div>
<div>
<button class="btn btn-primary" name="register" >Registreren</button>
</div>
</form>
This is the register code in the controller:
public function register()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
//voeg gebruiker toe aan database
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
'profiel_foto'=>$image
);
$this->db->insert('users',$data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
}
And these are the 2 errors I'm getting when I'm trying to registrate:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: ./upload/
Filename: controllers/Auth.php
Line Number: 131
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 131
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
A Database Error Occurred
Error Number: 1048
Column 'profiel_foto' cannot be null
INSERT INTO `users` (`voornaam`, `achternaam`, `email`, `wachtwoord`, `startdatum`, `postcode`, `huisnummer`, `woonplaats`, `beschrijving`, `geboortedatum`, `geslacht`, `profiel_foto`) VALUES ('hallo', 'hallo', 'hallo#gmail.com', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)
Filename: controllers/Auth.php
Line Number: 149
I modified your code. Try this
public function register() {
$data = array();
$config = array(
'upload_path' => 'upload',
'allowed_types' => 'gif|jpg|png|jpeg',
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('profiel_foto')) {
$error = array('error' => $this->upload->display_errors());
// var_dump( $error); die; check errors
} else {
$fileName = $this->upload->data();
$data['profiel_foto'] = $fileName['file_name'];
}
// voeg gebruiker toe aan database
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
);
$this->db->insert('users', $data);
$this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
redirect("auth/register", "refresh");
}
replace
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->input->post('profiel_foto');
$data_upload_files = $this->upload->data();
$image = $data_upload_files['./upload/'];
With
$target_dir = "upload/";
$target_file = $target_dir . time().basename($_FILES["profiel_foto"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imgName = time().basename($_FILES["profiel_foto"]["name"]);
move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);
Your insert function
$data = array (
'voornaam'=>$_POST['voornaam'],
'achternaam'=>$_POST['achternaam'],
'email'=>$_POST['email'],
'wachtwoord'=> ($_POST['wachtwoord']),
'startdatum'=>date('Y-m-d'),
'postcode'=>$_POST['postcode'],
'huisnummer'=>$_POST['huisnummer'],
'woonplaats'=>$_POST['woonplaats'],
'beschrijving'=>$_POST['beschrijving'],
'geboortedatum'=>$_POST['geboortedatum'],
'geslacht'=>$_POST['geslacht'],
'profiel_foto'=>$imgName
);
$this->db->insert('users',$data);
This question already has an answer here:
Upload Photo Failed Codeigniter
(1 answer)
Closed 6 years ago.
This is my code, my photo can't be inserted into the database. I don't know where the problem is.
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';
$config ['max_width'] = '2600';
$config ['max_height'] = '2200';
$id_soal = '';
$soal = $_POST['soal'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$kunci = $_POST['kunci'];
$status = $_POST['status'];
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/home/create_admin', $error);
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$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 :
Anyone can help?
you need to add the do_upload method of upload::library
$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);
//you need to add this do_upload();
if ( ! $this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$file_name = $this->upload->file_name;
.
.
.
...
}