Ifile not getting uploaded in the database

Multi tool use
Ifile not getting uploaded in the database
i'm trying to upload a file in databse using codeigniter, the image is getting stored in the folder but i'm having issue in storing the data in databse, i don know where
im going wrong. please can any one guide me what im doing wrong ? im new to codeigniter concept.
upload.php(controller)
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('filename')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data('filename'),$this->input->post());
$this->Upload_model->saverecords($data);
//$this->load->view('upload_success', $data);
}
}
}
?>
Upload_model.php(model)
<?php
class Upload_model extends CI_Model
{
//Insert
function saverecords($data)
{
//saving records
$this->db->insert('latest_news', $data);
}
}
?>
Upload_form.php(view)
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action = "" method = "POST">
<input type = "file" name = "filename" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
</form>
</body>
</html>
$this->input->post()
$data
sir m new to this codeigniter please can u explain me by editing my code?
– keerthi patil
Jul 3 at 8:01
that is why i am asking to print
$this->input->post()
– pradeep
Jul 3 at 8:01
$this->input->post()
Sorry sir im not getting u
– keerthi patil
Jul 3 at 8:06
1 Answer
1
You declare <form>
twice! Remove <form action = "" method = "POST">
completely and just use the above form_open_multipart
:
<form>
<form action = "" method = "POST">
form_open_multipart
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="filename" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Also there is no need for spaces like type = "submit"
type = "submit"
Then in your code:
if ( ! $this->upload->do_upload('filename')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
//$data = array('upload_data' => $this->upload->data('filename'),$this->input->post());
$data['upload_data'] = $this->upload->data('file_name');
$this->load->model('upload_model');
$this->upload_model->saverecords($data);
//$this->load->view('upload_success', $data);
}
For future:
Do not just do $this->input->post()
to assign the key value pairs to those in db. It is not very controlled and can lead to issues. Instead just define it. So if you add other data it would look something like:
$this->input->post()
$data = array(
'example1_fieldname' => $this->input->post('example1_fieldname');
'upload_data' => $this->upload->data('file_name');
);
where i should place the code $data = array(..........)
– keerthi patil
Jul 3 at 9:20
that is an example for the future. forget it for now until/if you add more inputs.
– Alex
Jul 3 at 9:22
no sir its not working
– keerthi patil
Jul 3 at 9:26
A PHP Error was encountered Severity: Notice Message: Undefined property: Upload::$Upload_model Filename: controllers/Upload.php Line Number: 30 Backtrace: File: C:xampphtdocsCodeIgniter_tryapplicationcontrollersUpload.php Line: 30 Function: _error_handler File: C:xampphtdocsCodeIgniter_tryindex.php Line: 315 Function: require_once
– keerthi patil
Jul 3 at 9:26
im getting such error
– keerthi patil
Jul 3 at 9:27
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
show your post
$this->input->post()
and add your post data individually in your$data
variable– pradeep
Jul 3 at 7:59