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();
Related
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();
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();
I have created a register form but whenever I try to insert data to database it dont give me error rather it stays on the same page with the filled data.I have created this in codeigniter.Below is my code.
Controller File: Home.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session','form_validation','email'));
$this->load->database();
$this->load->model('Sign');
}
public function index()
{
$this->load->view('home/index');
}
function signup()
{
$occu = $this->input->post('occupation');
$cf = $this->input->post('codefest');
$this->form_validation->set_rules('fname','Name','trim|required');
$this->form_validation->set_rules('mobile','Mobile No','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('password','Password','trim|required');
$this->form_validation->set_rules('cpass','Cofirm Password','trim|required|matches[password]');
$this->form_validation->set_rules('occupation','Occupation','trim|required|callback_select_validate');
$this->form_validation->set_rules('institute','Institute Name','trim|required');
$this->form_validation->set_rules('company','Company Name','trim|required');
$this->form_validation->set_rules('codefest','Codefest','trim|required|callback_select_codefest');
$this->form_validation->set_rules('address1','Address','trim|required');
$this->form_validation->set_rules('city','City','trim|required');
$this->form_validation->set_rules('pincode','Pincode','trim|required');
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('home/index');
}
else
{
//echo "inserted";
$data = array
('user_name' => $this->input->post('fname'),
'mobile_no' => $this->input->post('mobile'),
'user_email' => $this->input->post('email'),
'user_password' => $this->input->post('cpass'),
'occupation' => $this->input->post('occupation'),
'institute_name' => $this->input->post('institute'),
'company_name' => $this->input->post('company'),
'codefest_city' => $this->input->post('codefest'),
'address' => $this->input->post('address1'),
'city' => $this->input->post('city'),
'pincode' => $this->input->post('pincode'));
// insert form data into database
if ($this->Sign->insertUser($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your Message has been successfully received.We will back to you soon.</div>');
redirect('home/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('home/index');
}
}
}
// Below function is called for validating select option field.
function select_validate($occu)
{
if($occu=="none"){
$this->form_validation->set_message('select_validate', 'Please Select Your Occupation.');
return false;
} else{
// User picked something.
return true;
}
}
function select_codefest($cf)
{
if($cf=="none"){
$this->form_validation->set_message('select_codefest', 'Please Select a Codefest.');
return false;
} else{
// User picked something.
return true;
}
}
}
/* End of file Home.php */
/* Location: .//C/Users/CYBERBUFF/AppData/Local/Temp/fz3temp-1/Home.php */
Model File: Sign.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sign extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function insertUsers($data)
{
return $this->db->insert('signup',$data);
}
}
/* End of file Sign.php */
/* Location: .//C/Users/CYBERBUFF/AppData/Local/Temp/fz3temp-1/Sign.php */
View File : index.php
<div class="second-col col-md-6 col-sm-3">
<?php echo $this->session->flashdata('msg'); ?>
<div class="contact-area-left">
<h4>Sign Up</h4>
<?php echo form_open('home/signup', array('class' =>'comments-form contact-form'));?>
<div class="row" align="center">
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="fname" placeholder="Full Name" value="<?php echo set_value('fname');?>">
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="mobile" placeholder="Your Mobile No" value="<?php echo set_value('mobile');?>">
<span class="text-danger"><?php echo form_error('mobile'); ?></span>
</div>
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="email" placeholder="Email Address" value="<?php echo set_value('email');?>">
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group col-lg-5">
<input type="password" class="form-control" name="password" placeholder="Password" value="<?php echo set_value('password');?>">
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group col-lg-5">
<input type="password" class="form-control" name="cpass" placeholder="Confirm Password" value="<?php echo set_value('cpass');?>">
<span class="text-danger"><?php echo form_error('cpass'); ?></span>
</div>
<div class="form-group col-lg-10">
<select class="form-control" name="occupation" id="occupation" onchange="CheckContact(this.value);" >
<option value="none" selected="selected">------------Select Occupation------------</option>
<option value="student">Student</option>
<option value="professional">Professional</option>
</select>
<span class="text-danger"><?php echo form_error('occupation'); ?></span>
</div>
<div class="form-group col-lg-10" id="student" style="display: none;">
<input type="text" class="form-control" name="institute" placeholder="Institute Name" value="<?php echo set_value('institute');?>">
<span class="text-danger"><?php echo form_error('institute'); ?></span>
</div>
<div class="form-group col-lg-10" id="professional" style="display: none;">
<input type="text" class="form-control" name="company" placeholder="Company Name" value="<?php echo set_value('company');?>">
<span class="text-danger"><?php echo form_error('company'); ?></span>
</div>
<div class="form-group col-lg-10">
<select class="form-control" name="codefest">
<option value="none" selected="selected">------------Select Codefest------------</option>
<option value="cf_ agartala">Codefest Agartala</option>
<option value="cf_bhubaneswar">Codefest Bhubaneswar</option>
<option value="cf_chennai">Codefest Chennai</option>
<option value="cf_guwahati">Codefest Guwahati</option>
<option value="cf_kolkata">Codefest Kolkata</option>
</select>
<span class="text-danger"><?php echo form_error('codefest'); ?></span>
</div>
<div class="form-group col-lg-10">
<input type="text" class="form-control" name="address1" placeholder="Address" value="<?php echo set_value('address1');?>">
<span class="text-danger"><?php echo form_error('address1'); ?></span>
</div>
<!--<div class="form-group col-lg-5">
<input type="text" class="form-control" name="address2" placeholder="Address Line 2 (optional)" value="<?php echo set_value('address2');?>">
<span class="text-danger"><?php echo form_error('address2'); ?></span>
</div>-->
<div class="form-group col-lg-5">
<input type="text" class="form-control" name="city" placeholder="City" value="<?php echo set_value('city');?>">
<span class="text-danger"><?php echo form_error('city'); ?></span>
</div>
<div class="form-group col-lg-5">
<input type="text" class="form-control" name="pincode" placeholder="PIN Code" value="<?php echo set_value('pincode');?>">
<span class="text-danger"><?php echo form_error('pincode'); ?></span>
</div>
</div>
<div class="row" align="center">
<button class="btn btn-primary btn-lg">Register</button>
<button type="reset" class="btn btn-default btn-lg">Reset</button>
</div>
<?php echo form_close(); ?>
</div>
These required fields are hidden in form so validation fails;
<div class="form-group col-lg-10" id="student" style="display: none;">
<input type="text" class="form-control" name="institute" placeholder="Institute Name" value="<?php echo set_value('institute');?>">
<span class="text-danger"><?php echo form_error('institute'); ?></span>
</div>
<div class="form-group col-lg-10" id="professional" style="display: none;">
<input type="text" class="form-control" name="company" placeholder="Company Name" value="<?php echo set_value('company');?>">
<span class="text-danger"><?php echo form_error('company'); ?></span>
</div>
but validations are in controller
$this->form_validation->set_rules('institute','Institute Name','trim|required');
$this->form_validation->set_rules('company','Company Name','trim|required');
remove the validation if not needed. :)
i think i have correctly writting this code but i get a problem on missing argument and i selected file image to upload but i get error too like this.
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Data_guru::edit()
Filename: tim_monitoring/Data_guru.php
Line Number: 90
Backtrace:
File: C:\xampp\htdocs\sdb\application\controllers\tim_monitoring\Data_guru.php
Line: 90
Function: _error_handler
File: C:\xampp\htdocs\sdb\index.php
Line: 315
Function: require_once
You did not select a file to upload.
thanks for your answer
My Controller
public function edit($id) {
$this->form_validation->set_rules('nama_guru','Nama Guru','required');
$this->form_validation->set_rules('alamat','Alamat','required');
if ($this->form_validation->run() === FALSE) {
$data = array ('title' => 'Edit Data Guru',
'detail' => $this->monitoring_model->detail_guru($id),
'isi' => 'monitoring/edit_guru_view'
);
$this->load->view('monitoring/layout/wrapper',$data);
//Kalau Tidak Ada Error Data Guru DiUpdate
}else{
$config['file_name'] = $this->input->post('nama_guru');
$config['upload_path'] = './assets/image/guru/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$config['max_width'] = 3000;
$config['max_height'] = 3000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('foto_guru'))
{
echo $this->upload->display_errors();
}
else
{
$gbr = $this->upload->data();
$data = array(
'foto_guru' => $gbr['file_name'],
'id_guru' => $this->input->post('id_guru'),
'nama_guru' => $this->input->post('nama_guru'),
'jenis_kelamin' => $this->input->post('jen_kel'),
'alamat' => $this->input->post('alamat'),
'tempat_lahir' => $this->input->post('tempat_lahir'),
'tgl_lahir' => $this->input->post('tgl_lahir'),
'no_hp' => $this->input->post('no_hp'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$this->monitoring_model->edit_guru($data);
redirect(base_url().'tim_monitoring/data_guru');
}
}
}
My Model
//Menampilkan Detail Guru Di Halaman Edit Guru
public function detail_guru($id) {
$query = $this->db->get_where('t_guru', array('id_guru' => $id));
return $query->row_array();
}
//Update Data Guru Setelah Di Edit Di Halaman Edt
public function edit_guru($data) {
$this->db->where('id_guru',$data['id_guru']);
return $this->db->update('t_guru',$data);
}
My View
<form action="<?php echo base_url() ?>/tim_monitoring/data_guru/edit" class="form-horizontal" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Nama Guru</label>
<div class="col-sm-6">
<input type="text" name="nama_guru" class="form-control" placeholder="Nama Guru" value="<?php echo $detail['nama_guru'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Tempat Lahir</label>
<div class="col-sm-6">
<input type="text" name="tempat_lahir" class="form-control" placeholder="Tempat Lahir" value="<?php echo $detail['tempat_lahir'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Tanggal Lahir</label>
<div class="col-sm-6">
<input type="date" name="tgl_lahir" class="form-control" value="<?php echo $detail['tgl_lahir'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Alamat</label>
<div class="col-sm-6">
<textarea name="alamat" class="form-control" rows="4" required><?php echo $detail['alamat'] ?></textarea>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">No Handphone</label>
<div class="col-sm-6">
<input type="number" name="no_hp" class="form-control" placeholder="No Handphone" value="<?php echo $detail['no_hp'] ?>" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Foto</label>
<div class="col-sm-6">
<input type="file" name="foto_guru" id="exampleInputFile" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Username</label>
<div class="col-sm-6">
<input type="text" name="username" class="form-control" placeholder="Username" value="<?php echo $detail['username'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" placeholder="Password" value="<?php echo $detail['password'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Jenis Kelamin</label>
<div class="col-sm-6">
<input type="radio" name="jen_kel" value="L"><label> Laki - Laki</label>
<input type="radio" name="jen_kel" value="P"><label> Perempuan</label>
</div>
</div>
<input type="hidden" name="id_guru" class="form-control" value="<?php echo $detail['id_guru'] ?>" required>
<div class="form-group">
<div class="col-sm-4">
</div>
<div class="col-sm-6">
<button type="submit" class="btn btn-primary">Ubah Data</button>
</div>
</div>
</form>
Please see edit() Method of tim_monitoring/Data_guru.php that's must be require to pass one parameter as value of variable $id.
So, you need to add following URL in form's action
action="<?php echo base_url() ?>/tim_monitoring/data_guru/edit/<?php echo $detail['id_guru'] ?>"
For upload image form must require attribute enctype="multipart/form-data"
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>