Habib Rehman
Habib Rehman

Reputation: 618

Uploading Image in db using Codeigniter 3.0.1

i have read many forms/answer didn't helped me much on CI 3.0+. Trying to upload image path to mysql database. It seems like my Update query is not working (the image column doesn't have anything before running query). Here's my controller which's handling the upload thingy:

i have taken it from CI documentation upload.php

<?php

class Upload extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            // $this->load->helper(array('form', 'url'));
            $this->load->library('upload');
    }

    public function index()
    {
            $this->load->view('layouts/header');
            $this->load->view('home_page', array('error' => ' ' ));
            $this->load->view('layouts/footer');
    }

    public function do_upload()
    {

            $config['upload_path']          = './uploads/'; #$this->config->item('base_url').
            $config['allowed_types']        = 'gif|jpg|png|jpeg';
            // $config['max_size']             = 100;
            // $config['max_width']            = 1024;
            // $config['max_height']           = 768;

            // $this->load->library('upload', $config);

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors('<p>','</p>'));

                    $this->load->view('layouts/header');
                    $this->load->view('home_page', $error);
                    $this->load->view('layouts/footer');
            }

            else
            {

                    $this->load->model('model_edit');
                    $id = $this->session->userdata('id');
                    $data = array('upload_data' => $this->upload->data());
                    $image['img'] = $this->upload->data('file_name');


                    $this->model_edit->update_dp($id, $data);

                     #base_url().'upload/'.

                    $this->load->view('layouts/header');
                    // echo $id;
                    $this->load->view('home_page', $image);
                    $this->load->view('layouts/footer');
            }
    }
    }
    ?>

this is my model class model_edit.php

<?php

class Model_edit extends CI_Model
{

public function __construct()
{
    parent::__construct();
}
public function update_dp($id, $data) {

    $image_path = $this->upload->data('file_path');
    $sql = "UPDATE users SET images = '{$image_path}' WHERE id = '{$id}' LIMIT 1";
    $result = $this->db->query($sql);
    $row = $this->db->affected_rows();

    if ($row) {
        return $image_path;
    }
    else
    return FALSE;       
 } 
}
?>

This is the view i'm using home_page.php

<?php
$session_data = $this->session->userdata('logged_in');
$check = $this->session->userdata('remember_me');

?>
<div class="container">
<div class="row">
    <?php include('navbar.php'); ?>
</div>
<div class="row">
    <div class="col-xs-10">
        <?php

            if ($check) 
            {?>
            <div class="row">
              <div class="col-xs-4 col-xs-3">
              <?php if (isset($upload_data)) 
              {
                echo "Successfully Uploaded DP";
                echo $img;
                ?>
                <img src="<?php echo $img; ?>"/>
                <?php
              }?>               
                <?php if(isset($error))echo $error;  echo form_open_multipart('upload/do_upload');?>

                <input type="file" name="userfile"/>
                <br />
                <input type="submit" name="submit" value="upload" />

                </form>
              <hr>
              </div>
              <div class="col-xs-6 col-xs-5">

              </div>
            </div>
            <div class="row">  
              <div class="col-xs-4 col-xs-3">
              <label class="text-center"> User Information </label><a href="edit"><span class="glyphicon glyphicon-pencil pull-right"></span></a>
              <br><br>
                <label>Email: </label>
                <br>
                <?php echo $session_data['email'];?>
                <br><br>
                <label>Display Name: </label>
                <br>
                <?php echo $session_data['name'];?>
                <br><br>
                <label>Username: </label>
                <br>
                <?php echo $session_data['username'];?>
                <br><br>
                <a href="logout">Logout</a>
              </div>
             </div>

            <?php

            }
            else
            {
                // session_set_cookie_params(0);
                 include('browser_triger.php');
                 $session_data['username'];
                 $session_data['name'];
                 $session_data['email'];
            }
        ?>
    </div>
</div>

Upvotes: 2

Views: 4985

Answers (1)

Saty
Saty

Reputation: 22532

You need to get image path in controller and pass only image name to model file

In Controller

$image_path = $this->upload->data('file_name');
$this->model_edit->update_dp($id, $file_name);// pass image name

In Models

function update_dp($id, $file_name) {
    $this->db->set("images", $file_name);
    $this->db->where("id", $id);
    $this->db->update("users");
    $row = $this->db->affected_rows();

    if ($row) {
        return $image_path;
    } else{
        return FALSE;
    }
}

Upvotes: 1

Related Questions