使用代码点火器获取错误来显示要从url数据库查看的图像

时间:2016-12-08 11:18:09

标签: php sql codeigniter

嘿,伙计们我试图在我的数据库中显示和成像,但却出错了。关于它的有线部分如果我上传它上传的图像确定并且图像显示并且错误消失。请帮我摆脱错误肯定。我认为问题出在会议上,但我不知道请看看。 这是我的profile_view:

<!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>
<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];;?>">

<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
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;
        $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();
            $images_name = $upload_data['file_name'];
            //update user photo intro database
            $this->user_model->add_image($images_name, $this->session->userdata('uid'), $upload_data['file_name']);

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

    }

    }
}

这是我的用户模型:

<?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($uemail, $id)
    {


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

    }

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

}


?>

更新:这是错误,请查看链接http://imgur.com/gFC7A6q

2 个答案:

答案 0 :(得分:1)

它告诉你$upload_data['file_name']不存在。

我认为这是因为你没有上传任何内容,那么当你这样做时 - 它存在并且一切都很好。

所以,你可以换掉

<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];;?>">

有:

<?php if(isset( $upload_data['file_name'] )){ ?> 
<img alt="Your uploaded image" src="<?=base_url(). 'assets/img/' . $upload_data['file_name'];?>">
<?php } ?>

在尝试应用之前检查是否已设置。

答案 1 :(得分:0)

发现问题应该在配置文件控制器中修复它:)

function index()
    {
        if (!isset($details[0])){
            $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;
            $this->load->view('profile_view', $data);}