CodeIgniter - Update Database Record - php

I've done the coding of Update Database record in Codeigniter. Everything works fine but When I click the Save button database records won't update. Each time it's showing the old database records. I want to update the database records but what's wrong with this. I don't understand the missing thing. Help Needed.
My Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
function __construct()
{
parent::__construct();
if($this->session->email == "")
{
redirect('login');
}
$this->load->model('EmployeeModel','EmployeeModel');
}
public function ViewEmployee()
{
$data['getEmployee'] = $this->EmployeeModel->getEmployee();
$this->load->view('view_employee',$data);
}
public function update($empID) {
$data['user'] = $this->EmployeeModel->get_by($empID);
$data['subview'] = 'Employee/edit_employee';
$this->load->view('edit_employee', $data);
}
public function edit() {
$empID = $this->input->post('empID');
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
}else{
$picture = '';
}
}else{
$picture = '';
}
$data = array(
'empFirstName' => $this->input->post('firstname'),
'empLastName' => $this->input->post('lastname'),
'empNO' => $this->input->post('employee_id'),
'designation' => $this->input->post('designation'),
);
$this->db->where('empID', $empID);
$this->db->update('tbl_employee', $data);
redirect('view_employee');
$this->update($empID);
}
}
My Model:
<?php
class EmployeeModel extends CI_Model
{
function __construct() {
parent::__construct();
}
public function EmployeeRegister($data)
{
$this->db->insert('tbl_employee',$data);
return true;
}
public function getEmployee()
{
$this->db->where('status',0);
$query = $this->db->get('tbl_employee');
return $query->result();
}
public function get_by($empID) {
$this->db->where('empID', $empID);
$user = $this->db->get('tbl_employee')->row();
return $user;
}
}
View:
<?php
print_r($user)
?>
<?php echo form_open('Employee/edit'); ?>
<div class="row">
<div class="form-titles"> Personal Information</div>
<div class="col-lg-4 input_field_sections">
<h5>Employee Name <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="firstname" value="<?=$user->empFirstName?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Father's Name <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="lastname" value="<?=$user->empLastName?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>NIC <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="cnic" value="<?=$user->cnic?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Date of Birth <span style="color: #cc0000">*</span></h5>
<input required="" type="text" class="form-control datepicker" name="birth_date" value="<?=$user->birth_date?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>Mobile #<span style="color: #cc0000">*</span></h5>
<input required="" type="text" class="form-control" name="mobile" value="<?=$user->empMobile?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Phone #</h5>
<input type="text" class="form-control" name="phone" value="<?=$user->phone?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>Email ID</h5>
<input type="email" class="form-control" name="email" value="<?=$user->empemail?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Current Photo</h5>
<input type="file" class="form-control" name="picture" value="<?=$user->picture?>"/>
<img src="<?php echo base_url().'uploads/'.$user->picture?>" style="width: 150px;height: auto;" />
</div>
</div>
<div class="row">
<div class="col-lg-8 input_field_sections">
<h5>Address / City <span style="color: #cc0000">*</span></h5>
<textarea required="" name="address" class="addres form-control" /><?=$user->empaddress?></textarea>
</div>
</div>
<div class="row">
<div class="form-titles" style="margin-top:40px;"> Job Information</div>
<div class="col-lg-4 input_field_sections">
<h5>Work Location <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="worklocation" value="<?=$user->worklocation?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Employee ID <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="employee_id" value="<?=$user->empNO?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>Department <span style="color: #cc0000">*</span></h5>
<input required="" type="text" class="form-control" name="department" value="<?=$user->department?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Designation <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="designation" value="<?=$user->designation?>"/>
</div>
</div>
<div class="col-lg-8 input_field_sections">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>

Use this one to debug your application.
echo $this->db->last_query();

Related

upload profile photo not working in codeigniter 3

i want to allow my user to update their profile and profile photo. if i comment the profile photo part in view and get rid in controller. the edit went well, but if i put the upload part in controller, the edit function not working. btw i'm still new in codeigniter. can you help me find my mistake.. thank you
my controller
public function edit($id){
$this->load->model('User_model');
$user = $this->User_model->getUser($id);
$data = array();
$data['user'] = $user;
$data1['user'] = $this->db->get_where('user', ['email' =>
$this->session->userdata('email')])->row_array();
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[8]|matches[password2]', [
'matches' => 'password not match!',
'min_length' => 'password too short'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$this->load->view('templates/admin/header', $data, $data1);
$this->load->view('templates/admin/sidebar', $data, $data1);
$this->load->view('templates/admin/topbar', $data1);
$this->load->view('admin/updateprofile', $data, $data1);
$this->load->view('templates/admin/footer', $data, $data1);
} else {
$upload_image = $_FILES['image']['name'];
if ($upload_image) {
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['upload_path'] = './assets/img/proile/';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$old_image = $data['user']['image'];
if ($old_image != 'default.jpg') {
unlink(FCPATH . 'assets/img/profile/' . $old_image);
}
$new_image = $this->upload->data('file_name');
$this->db->set('image', $new_image);
} else {
echo $this->upload->display_errors();
}
}
$data = array(
'name' => htmlspecialchars($this->input->post('name', true)),
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
);
$this->User_model->updateUser($id, $data);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">
User update successfully!
</div>');
redirect('adminprofile');
}
}
}
my model
function getUser($id)
{
$this->db->where('id', $id);
return $user = $this->db->get('user')->row_array();
}
function updateUser($id, $data)
{
$this->db->where('id', $id);
$this->db->update('user', $data);
}
my view
<div class="container-fluid">
<h1 class="h3 mb-4 text-gray-800">Edit Profile</h1>
<div class="row">
<div class="col-lg-8">
<form class="user" method="post" action="<?php echo base_url() . 'AdminUpdateProfile/edit/' . $user['id']; ?>">
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Nama</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="name" name="name" value="<?= set_value('name', $user['name']); ?>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Emel</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="<?= set_value('email', $user['email']); ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Kata Laluan</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password1" name="password1" placeholder="8 aksara">
<?= form_error('email', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Ulang Kata Laluan</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password2" name="password2" placeholder="Masukkan semula">
<?= form_error('email', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
<div class="form-group row">
<div class="col-sm-2">Gambar Profil</div>
<div class="col-sm-10">
<div class="row">
<div class="col-sm-3">
<img src="<?= base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
</div>
<div class="col-sm-9">
<div class="custom-file">
<input type="file" class="custom-file-input" id="image" name="image">
<label class="custom-file-label" for="image">Choose file</label>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row justify-content-end">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!--<div class="container">
<div class="card o-hidden border-0 shadow-lg my-5 col-lg-7 mx-auto">
<div class="card-body p-0">
<div class="row">
<div class="col-lg">
<div class="p-5">
<div class="text-center">
<img src="<//?= base_url('assets/img/profile/') . $user['image']; ?>">
</div>
<hr>
<?= $this->session->set_flashdata('message'); ?>
<form class="user" method="post" action="<?php echo base_url() . 'AdminUpdateProfile/edit/' . $user['id']; ?>">
<div class="form-group row">
<input type="text" class="form-control" id="name" name="name" value="<?= set_value('name', $user['name']); ?>">
//<?= form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class=" form-group row">
<input type="text" class="form-control" id="email" name="email" value="<?= set_value('email', $user['email']); ?>" readonly>
<?= form_error('email', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group row">
<input type="password" class="form-control" id="password1" name="password1" placeholder="Password">
<?= form_error('password1', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group row">
<input type="password" class="form-control" id="password2" name="password2" placeholder="Repeat Password">
</div>
<button type="submit" class="btn btn-primary btn-user btn-block">
Update
</button>
Cancel
</div>
</form>
</div>
</div>
</div>
</div> -->
First you have to use this after action attribute in form tag :
enctype='multipart/form-data'
You need to specify the enctype just like Aman said
<form class="user" method="post" action="<?= base_url('AdminUpdateProfile/edit/'.$user['id']); ?>" enctype="multipart/form-data">
your code here
</form>
As two of the above answers mention, first you need to add the encryption type to your form. So the end result would be:
<form method="post" enctype="multipart/form-data" class="user" action="<?php echo base_url() . 'AdminUpdateProfile/edit/' . $user['id']; ?>">
If this still doesn't work, in my case I had to convert the $_FILES indexes to $_FILES['userfile'] because of an issue with the upload library where it had issues with indexes that weren't userfile. I basically did this:
$this->load->library('upload');
$config['upload_path'] = 'images/';
$config['file_ext_tolower'] = TRUE;
$config['allowed_types'] = '*';
$this->upload->initialize($config);
$_FILES['userfile']['name'] = $_FILES['image']['name'];
$_FILES['userfile']['type'] = $_FILES['image']['type'];
$_FILES['userfile']['tmp_name'] = $_FILES['image']['tmp_name'];
$_FILES['userfile']['error'] = $_FILES['image']['error'];
$_FILES['userfile']['size'] = $_FILES['image']['size'];
$this->upload->do_upload();
$data = $this->upload->data();

File uploading issue in Codeigniter

I have this error in controller
A PHP Error was encountered
Severity: Notice
Message: Undefined property: dashboardController::$upload
Filename: controllers/dashboardController.php
Line Number: 70
Call to a member function data() on a non-object in
C:\xampp\htdocs\High_tack\application\controllers\dashboardC‌​ontroller.php
on line 71
just do as i done it in my project using wamp and codeigniter. I uploaded my pictures directly into the folder named uploads in the project folder.
here is my controller named addproduct,
<?php class Addproduct extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('addproduct');
}
function save() {
$name_file = $_FILES['pro_filename']['name'];
//$user=$this->session->userdata['logged_in'];
$data=array (
'pro_name'=>$this->input->post('pro_name'),
'pro_code'=>$this->input->post('pro_code'),
'hsn_code'=>$this->input->post('hsn_code'),
'pro_price'=>$this->input->post('pro_price'),
'pro_tax1'=>$this->input->post('pro_tax1'),
'pro_tax2'=>$this->input->post('pro_tax2'),
'pro_tax3'=>$this->input->post('pro_tax3'),
'pro_tax4'=>$this->input->post('pro_tax4'),
'pro_description'=>$this->input->post('pro_description'),
'pro_brand'=>$this->input->post('pro_brand'),
'pro_category'=>$this->input->post('pro_category'),
'pro_scategory'=>$this->input->post('pro_scategory'),
'pro_sscategory'=>$this->input->post('pro_sscategory'),
'pro_qauntity'=>$this->input->post('pro_qauntity'),
'pro_service'=>$this->input->post('pro_service'),
'pro_certificate'=>$this->input->post('pro_certificate'),
'pro_filename'=>$name_file,
'pro_date'=>$this->input->post('pro_date')
);
$this->load->model('base_model');
$this->base_model->save('addproduct',$data);
$this->do_upload();
$this->load->view('addproduct');
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|gif|jpeg';
$config['max_size'] = 500000;
$config['max_width'] = 200000;
$config['max_height'] = 200000;
$this->load->library('upload', $config);
$this->upload->do_upload('pro_filename');
}
}
here is my view page named addproduct
<div class="panel-body">
<div class="form">
<div class="col-md-12 sign-up text-left">
<div class="form-group ">
<label for="pro_name" class="control-label col-lg-3">Product Name</label>
<div class="col-lg-6">
<input class="form-control " id="pro_name" name="pro_name" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_code" class="control-label col-lg-3">Product Code</label>
<div class="col-lg-6">
<input class="form-control " id="pro_code" name="pro_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="hsn_code" class="control-label col-lg-3">HSN Code</label>
<div class="col-lg-6">
<input class="form-control " id="hsn_code" name="hsn_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_price" class="control-label col-lg-3">Prize</label>
<div class="col-lg-6">
<input class="form-control " id="pro_price" name="pro_price" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax1" class="control-label col-lg-3">Tax 1</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax1" name="pro_tax1" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax2" class="control-label col-lg-3">Tax 2</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax2" name="pro_tax2" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax3" class="control-label col-lg-3">Tax 3</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax3" name="pro_tax3" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax4" class="control-label col-lg-3">Tax 4</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax4" name="pro_tax4" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_description" class="control-label col-lg-3">Description</label>
<div class="col-lg-6">
<input class="form-control " id="pro_description" name="pro_description" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_brand" class="control-label col-lg-3">Brand</label>
<div class="col-lg-6">
<input class="form-control " id="pro_brand" name="pro_brand" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_category" class="control-label col-lg-3">Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_category" name="pro_category" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_scategory" class="control-label col-lg-3">Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_scategory" name="pro_scategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_sscategory" class="control-label col-lg-3">S-Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_sscategory" name="pro_sscategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_quantity" class="control-label col-lg-3">Qauntity</label>
<div class="col-lg-6">
<input class="form-control " id="pro_quantity" name="pro_quantity" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_service" class="control-label col-lg-3">Service Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_service" name="pro_service" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_certificate" class="control-label col-lg-3">Certificate Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_certificate" name="pro_certificate" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_filename" class="control-label col-lg-3">Photo:</label>
<div class="col-lg-6">
<input class="form-control" type="file" name="pro_filename" required/>
</div>
</div><br><br>
<div class="clearfix"></div>
<?php
$today = date("Y-m-d");
?>
<input type="hidden" name="pro_date" value="<?php echo $today ?>">
<div class="form-group">
<div class="col-lg-offset-3 col-lg-6">
<button class="btn btn-primary" type="submit">Save</button>
<button class="btn btn-default" >Cancel</button>
</div>
</div>
<br>
</div>
<br>
</div>
</div>
this is my code in model named base_model
public function save($table, $data) {
return $this->db->insert($table, $data);
}
delete the portion you dont want, if you dont need to upload the image to folder simply change the controller field where the upload occurs
'pro_filename'=>$this->input->post('pro_filename'),

Calling controllers different methods from view page using codeigniter

I am working on codeigniter project in which viwe page having textboxes, radio buttons and button.I fetched textbox values from database and manually enter from date and to date then by clicking on save button insert query gets empty values because in controller i called all functions serially in index function.What is the best solution to call controller methods on each event of view page?
leave_appl_view.php
<form action="leave_apply_control/index" method="post" id="myForm">
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Emp ID</label>
<?php if(!empty($emp_leave))
{
foreach ($emp_leave as $liv)
{ ?>
<input type="text" class="form-control" name="id" value="<?php echo $liv->employee_id ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Employee Name</label>
<input type="text" class="form-control" name="empname" id="empname" value="<?php echo $liv->employee_name ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Email address</label>
<input type="email" class="form-control" name="empmail" id="empmail" value="<?php echo $liv->email ?>" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Leave type: </label>
<input type="radio" id="half" name="type" onclick="Checkradiobutton()" value="half"> Half Day
<input type="radio" id="full" name="type" onclick="Checkbutton()" value="full" Checked> Full Day
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="frm_dt">
<label class="control-label">From Date: </label>
<input type="text" class="form-control" id="datepicker1" name="from" ></p>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="to_dt">
<label class="control-label">To Date: </label>
<input type="text" class="form-control" id="datepicker2" name="to" ></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Balance Leaves</label>
<input type="text" class="form-control" name="bal_leave" value="<?php echo $liv->balanced_leaves ?>" >
</div>
<?php }
}
?>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Leaves</label>
<input type="text" class="form-control" name="totaldays" id="totaldays" readonly>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Date</label>
<input type="text" class="form-control" name="todayDate" id="todayDate" value="<?php echo date("d/m/Y"); ?>" readonly>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
<div class="form-group label-floating">
<label class="control-label">Reason </label>
<TEXTAREA class="form-control" id="reason" name="reason"> </TEXTAREA>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" id="submit" name="submit" onclick="<?php // echo base_url() ?>leave_apply_control/save_data">Apply</button>
<button type="reset" class="btn btn-primary" onclick="<?php echo base_url();?>leave_apply_control" >Reset</button>
</form>
leave_Apply_control.php
<?php
class leave_apply_control extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->library('calendar');
$this->load->database();
$this->load->model('user_model');
$this->load->helper('date');
}//contruct close
function index()
{
$id=$this->session->userdata('id');
$data['foren_id'] = $this -> user_model -> get_emp_id($id);
foreach($data['foren_id'] as $fk)
{
//echo "////control page fk is ".$fk->emp_id;
}
$data2['emp_leave'] = $this->user_model->get_leave_count($fk->emp_id);
$this->load->view('leave_application',$data2);
$this->save_data();
}
public function save_data()
{
$fd = date('Y-m-d',strtotime($this->input->post('from')));
$td = date('Y-m-d',strtotime($this->input->post('to')));
$data = array(
'employee_id' => $this->input->post('id'),
'emp_name' => $this->input->post('empname'),
'leave_from' => $fd,
'leave_to' => $td,
'no_of_days' => $this->input->post('totaldays'),
'applied_date' => date('Y-m-d H:i:s'),
'reason' => $this->input->post('reason'),
'mail' => $this->input->post('empmail'),
'leave_type' => $this->input->post('type'),
'balance_leaves' => $this->input->post('bal_leave'),
'status' => 1,
);
$this->user_model->insert_leave($data);
}
}

Uploading Image File in Codeigniter 3 returns error 500

So I'm trying to insert in the database and upload a file. But when I'm trying to submit, it returns a server error.
The same code is working on my other project but it is using CI2, i checked the .htaccess and its all the same
The localhost page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
I think the problem is in the $this->upload->do_upload('userfile') because i tried to comment out from this part and display echo the config array and there's no error but when i tried to uncomment up to this part, the error shows.
Controller
public function add_now(){
$this->load->library('form_validation');
$this->form_validation->set_rules('Event_Name', 'Event Name', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Start', 'Start Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_End', 'End Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Location', 'Location', 'trim|strip_tags');
if($this->form_validation->run() == FALSE){
$this->add();
}
else{
$query = $this->events_model->do_upload();
if($query){
$this->session->set_flashdata('success', 'Successful!');
$this->index();
}
else{
if(!$this->session->flashdata('upload_error')){
$this->session->set_flashdata('failed', 'Failed!');
}
$this->add();
}
}
}
Model
public function do_upload(){
$config['upload_path'] = './resources/images/events_photo/temp/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['file_name'] = uniqid().'.jpeg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')){
$this->session->set_flashdata('upload_error', $this->upload->display_errors());
}
}
View
<?php echo form_open_multipart('events/add_now'); ?>
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="Event_Name" value="<?php echo set_value('Event_Name'); ?>" placeholder="Enter Event Name">
<small class="text-danger"><?php echo form_error('Event_Name'); ?></small>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">Start Date</label>
<input type="text" class="form-control" name="Event_Start" value="<?php echo set_value('Event_Start'); ?>" placeholder="Enter Start Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_Start'); ?></small>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">End Date</label>
<input type="text" class="form-control" name="Event_End" value="<?php echo set_value('Event_End'); ?>" placeholder="Enter End Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_End'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Location</label>
<input type="text" class="form-control" name="Event_Location" value="<?php echo set_value('Event_Location'); ?>" placeholder="Enter Event Location">
<small class="text-danger"><?php echo form_error('Event_Location'); ?></small>
</div>
</div>
</div>
<div class="row">
<textarea placeholder="Event Description" name="Event_Description" rows="10" class="form-control"></textarea>
</div>
<div class="row">
<div class="form-group">
<label class="control-label">Image Attachment (Optional)</label>
<input type="file" class="form-control" name="userfile" value="<?php echo set_value('userfile'); ?>" placeholder="Upload Image">
<?php if($this->session->flashdata('upload_error')): ?>
<small class="text-danger"><?php echo $this->session->flashdata('upload_error'); ?></small>
<?php endif; ?>
</div>
</div>
</div>
<div class="panel-footer text-right">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</form>

Codeigniter File Upload and Database Insert In 1 form

So I'm trying to insert in the database and upload a file. But when I'm trying to submit, it returns a server error.
The localhost page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
I think the problem is in the $this->upload->do_upload('userfile') because i tried to comment out from this part and display echo the config array and there's no error but when i tried to uncomment up to this part, the error shows.
Controller
public function add_now(){
$this->load->library('form_validation');
$this->form_validation->set_rules('Event_Name', 'Event Name', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Start', 'Start Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_End', 'End Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Location', 'Location', 'trim|strip_tags');
if($this->form_validation->run() == FALSE){
$this->add();
}
else{
$query = $this->events_model->do_upload();
if($query){
$this->session->set_flashdata('success', 'Successful!');
$this->index();
}
else{
if(!$this->session->flashdata('upload_error')){
$this->session->set_flashdata('failed', 'Failed!');
}
$this->add();
}
}
}
Model
public function do_upload(){
$config['upload_path'] = './resources/images/events_photo/temp/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['file_name'] = uniqid().'.jpeg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')){
$this->session->set_flashdata('upload_error', $this->upload->display_errors());
}
}
View
<?php echo form_open_multipart('events/add_now'); ?>
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="Event_Name" value="<?php echo set_value('Event_Name'); ?>" placeholder="Enter Event Name">
<small class="text-danger"><?php echo form_error('Event_Name'); ?></small>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">Start Date</label>
<input type="text" class="form-control" name="Event_Start" value="<?php echo set_value('Event_Start'); ?>" placeholder="Enter Start Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_Start'); ?></small>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">End Date</label>
<input type="text" class="form-control" name="Event_End" value="<?php echo set_value('Event_End'); ?>" placeholder="Enter End Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_End'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Location</label>
<input type="text" class="form-control" name="Event_Location" value="<?php echo set_value('Event_Location'); ?>" placeholder="Enter Event Location">
<small class="text-danger"><?php echo form_error('Event_Location'); ?></small>
</div>
</div>
</div>
<div class="row">
<textarea placeholder="Event Description" name="Event_Description" rows="10" class="form-control"></textarea>
</div>
<div class="row">
<div class="form-group">
<label class="control-label">Image Attachment (Optional)</label>
<input type="file" class="form-control" name="userfile" value="<?php echo set_value('userfile'); ?>" placeholder="Upload Image">
<?php if($this->session->flashdata('upload_error')): ?>
<small class="text-danger"><?php echo $this->session->flashdata('upload_error'); ?></small>
<?php endif; ?>
</div>
</div>
</div>
<div class="panel-footer text-right">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</form>
First load the url helper in your controller...
$this->load->helper('url');
Then set the upload file path as:
$config['upload_path'] = base_url('resources/images/events_photo/temp/');
Try it might works.
And what about these two functions
$this->add();
$this->success();

Categories