I have a problem with my CodeIgniter project. The picture I am uploading, is only added to the folder, but not in database. Column name in database is "user_image".
This is my Controller:
public function upload() {
if(!$this->session->userdata('logged_in')) {
redirect('users/login');
}
$data['title'] = "file Upload";
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 500;
$this->load->library('upload', $config);
$data['error'] = "";
if ( ! $this->upload->do_upload('userfile'))
{
if(isset($_FILES['userfile'])){
$data['error'] = $this->upload->display_errors();
}
$this->load->view('templates/header');
$this->load->view('users/upload', $data);
$this->load->view('templates/footer');
}
else
{
$user_id = $this->session->userdata('id');
$uploaddata = $this->upload->data();
$filename = $uploaddata['file_name'];
$userdata = array(
'user_image' => $filename
);
$this->user_model->update($user_id, $userdata);
$this->session->set_flashdata('message', "Upload Succesfully");
redirect('dashboard/index');
}
}
My model:
public function update($user_id, $userdata) {
$this->db->where('id', $user_id);
$this->db->update('users', $userdata);
}
And my view:
<?php echo form_open_multipart('users/upload'); ?>
<div >
<div >
<h1 ><?php echo $title; ?></h1>
<div >
<input type="file" name="userfile" >
<?php echo $error; ?>
</div>
<br>
<br>
<div >
<button type="submit" >Upload</button>
</div>
</div>
</div>
<?php echo form_close(); ?>
Thank you very much in advance guys. I will appreciate any kind of help.
CodePudding user response:
You can try by returning a single item from array.
$filename = $this->upload->data('file_name'); // i.e. it will return test.jpg
Also check your database column type to accept / save filename as string
CodePudding user response:
Code above does not seem to have any obvious errors, but there are few items to check:
- is user model really wired to database? https://codeigniter.com/userguide3/general/models.html#connecting-to-your-database
- does
users
table really haveuser_image
field in schema? - does
session->userdata('id')
gets updated with real existing user id upon login? - does record for user with given
id
really already exists in database?update()
is not inserting new values. https://codeigniter.com/userguide3/database/query_builder.html#updating-data