如何使用代码点火器将图像路径添加到数据库

时间:2016-12-08 07:46:22

标签: php sql codeigniter

如何使用代码点火器将图像路径添加到数据库,到目前为止,我有这个,我没有得到任何错误,但它没有将图像URL添加到数据库。

这是我的个人资料控制器:

<?php
class Profile extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url', 'html', 'form'));
        $this->load->library(array('session', 'form_validation'));
        $this->load->database();
        $this->load->model('user_model');


    }

    function index()
    {
        $details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
        $data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
        $data['uemail'] = $details[0]->email;
        $data['uimage'] = $details[0]->image;
        $this->load->view('profile_view', $data);

    }

    function delete()
    {
        $this->user_model->delete_row($this->session->userdata('uid'));
        redirect('Site/index');

    }

    function update()
    {

        $this->user_model->update($this->input->post("name"), $this->session->userdata('uid'));
        $this->user_model->update($this->input->post("email"), $this->session->userdata('uid'));
        $this->user_model->add_image($this->input->post('userfile'), $this->session->userdata('uid'));
        redirect('profile/index');

    }


    public function index1()
    {
        $this->load->view('profile_view', array('error' => ' '));
    }

    public function do_upload() {
        $config['upload_path']          = './assets/img';
        $config['allowed_types']        = 'gif|jpg|png';

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


        $details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
        $data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
        $data['uemail'] = $details[0]->email;

        if ( ! $this->upload->do_upload('userfile'))
        {
            $data['error'] = $this->upload->display_errors();
            $this->load->view('profile_view', $data);
        }
        else
        {
            $upload_data = $this->upload->data();
            $data['image']= $upload_data['file_name'];
            $data['uid'] = $this->session->userdata('uid');
            $this->load->view('profile_view', $data);
        }

        }
    }

这是我的个人资料视图:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="<?php echo base_url('css/mystyle.css'); ?>" type="text/css"/>
    <link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/mystyle.css');?>" />
    <link rel="stylesheet" href="<?php echo base_url()?>assets/css/mystyle.css" type="text/css">
    <?php
    $autoload['helper'] = array('css_js');?>

</head>
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <div class="mascot">
                <a class="navbar-brand" href="#">
                    <img src="<?php echo base_url()?>assets/img/monstercode.png" alt="Green Monster Mascot" style="width:160px;height:32px;"></a>
            </div>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="<?php echo site_url('')?>">Home</a></li>
                <li><a href="<?php echo site_url('Site/sites')?>">Projects</a></li>
                <li><a href="<?php echo site_url('Site/developers')?>">Developers</a></li>
                <li><a href="<?php echo site_url('Site/employers')?>">Employers</a></li>
                <li><a href="#">Contact</a></li>

                <?php if ($this->session->userdata('login')){ ?>

                    <li class="right nav navbar-nav navbar-right"><p class="navbar-text">Hello <?php echo $this->session->userdata('uname'); ?></p></li>
                    <li class="right nav navbar-nav navbar-right"><a href="<?php echo base_url(); ?>index.php/home/logout">Log Out</a></li>
                <?php } else { ?>
                    <li><a href="<?php echo base_url(); ?>index.php/login">Login</a></li>
                    <li><a href="<?php echo base_url(); ?>index.php/signup">Signup</a></li>
                <?php } ?>
            </ul>
        </div>
    </div>
</nav>

<div class="container-fluid text-center">
    <div class="row content">
        <div class="col-sm-2 sidenav">
            <div class="well">
                <p>User Picture <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
            </div>
            <?php echo form_open_multipart('profile/do_upload'); ?>
            <?php echo form_upload('userfile'); ?><br />
            <?php echo form_submit('upload', 'Upload');?>
            <?php echo form_close(); ?>
            <div class="well">
                <p>User Info<br/> Name: <?php echo $uname; ?><br/> Email: <?php echo $uemail; ?> <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
            </div>
        </div>
        <div class="col-sm-8 text-left">


            <div class="container">
                <div class="row">
                    <div class="col-md-4">
                        <h4>Profile Summary</h4>
                        <hr/>
                        <form method="post" action="<?php echo site_url('profile/update');?>">
                            <p>Name: <input type="text" name='name'value="<?php echo $uname; ?>"></p>
                            <p>Email:  <input type="text" name='email' value="<?php echo $uemail; ?>"></p>
                            <button type="submit" class="btn btn-default">Update</button>
                        </form>
                        <form method="post" action="<?php echo site_url('profile/delete');?>">
                            <button type="submit" class="btn btn-default">Delete</button>
                        </form>
                        <p>...</p>
                    </div>
                    <div class="col-md-8">
                        <p>lorem ipsum dolum</p>
                        <p>lorem ipsum dolum</p>
                        <p>lorem ipsum dolum</p>
                        <p>...</p>
                    </div>
                </div>
            </div>
            <hr>
            <h3>More User Info</h3>
            <p>Lorem ipsum...</p>
        </div>
        <div class="col-sm-2 sidenav">
            <div class="well">
                <p>Job Offers <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
            </div>
            <div class="well">
                <p>Job Offers <br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="<?php echo base_url("assets/js/jquery-1.10.2.js"); ?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/bootstrap.js"); ?>"></script>
</body>
</html>

这是我的模型代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function get_user($email, $password)
    {
        $this->db->where('email', $email);
        $this->db->where('password', $password);
        $query = $this->db->get('user');
        /* Select * from users where email = $_Post['email'] and password = $_post['password'] */
        return $query->result();
    }

    // get user
    function get_user_by_id($id)
    {
        $this->db->where('id', $id);
        //select id from users where id = $_Post['id']
        $query = $this->db->get('user');
        return $query->result();
    }

    // insert
    function insert_user($data)
    {
        return $this->db->insert('user', $data);
    }


    //delete row
    function delete_row($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('user');
    }

    function update($uimage, $uemail, $id)
    {


        $this->db->where('id', $id);
        $this->db->set('email', $uemail);
        $this->db->set('image', $uimage);
        $this->db->update('user');

    }
}

请告诉我如何使其正常工作,感谢您的支持。

1 个答案:

答案 0 :(得分:1)

在更新模型中添加方法:

function add_image($uimage, $id){
    $this->db->where('id', $id);
    $this->db->set('image', $uimage);
    $this->db->update('user');
}

上传用户照片后调用add_image方法:

public function do_upload() {
    $config['upload_path']          = './assets/img';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 100;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;

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


    $details = $this->user_model->get_user_by_id($this->session->userdata('uid'));
    $data['uname'] = $details[0]->firstname . " " . $details[0]->lastname;
    $data['uemail'] = $details[0]->email;

    if ( ! $this->upload->do_upload('userfile'))
    {
        $data['error'] = $this->upload->display_errors();
        $this->load->view('profile_view', $data);
    }
    else
    {       
        $upload_data = $this->upload->data(); 
        $images_name = $upload_data['file_name'];

        //update user photo intro database
        $this->user_model->add_image($images_name, $this->session->userdata('uid'));

        $data['upload_data'] = $upload_data;
        $data['uid'] = $this->session->userdata('uid')
        $this->load->view('profile_view', $data);
    }
}