使用crud将图片上传到codeigniter错误 [英] Image upload to codeigniter error using crud
本文介绍了使用crud将图片上传到codeigniter错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿家伙得到错误未定义的变量见图片 http://imgur.com/oJMbrYo p>
我想做的是上传一个图像,然后将其发送到数据库的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 - > session-> userdata('uid'));
$ data ['uname'] = $ details [0] - > firstname。 。 $ details [0] - > lastname;
$ data ['uemail'] = $ details [0] - >电子邮件;
$ 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'))
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';
$ config ['max_size'] = 100;
$ config ['max_width'] = 1024;
$ config ['max_height'] = 768;
$ this-> load-> library('upload',$ config);
if(!$ this-> upload-> do_upload('userfile'))
{
$ error = array('error'=> $ this- > upload-> display_errors());
$ this-> load-> view('profile_view',$ error);
}
else
{
$ data = array('upload_data'=> $ this-> upload-> data(),$ this-> session- > userdata('uid'));
$ this-> load-> view('profile_view',$ data);
}
}
`}
:
?>
<!DOCTYPE html>
< html lang =en>
< head>
< title> Bootstrap示例< / title>
< meta charset =utf-8>
< meta name =viewportcontent =width = device-width,initial-scale = 1>
< link rel =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.csstype =text / css>
< link rel =stylesheethref =<?php echo base_url('css / mystyle.css');?> type =text / css/>
< link rel =stylesheettype =text / csshref =< ;? echo base_url('assets / css / mystyle.css');?> />
< link rel =stylesheethref =<?php echo base_url()?> assets / css / mystyle.csstype =text / css>
<?php
$ autoload ['helper'] = array('css_js');?>
< / head>
< nav class =navbar navbar-inverse>
< div class =container-fluid>
< div class =navbar-header>
< button type =buttonclass =navbar-toggledata-toggle =collapsedata-target =#myNavbar>
< span class =icon-bar>< / span>
< span class =icon-bar>< / span>
< span class =icon-bar>< / span>
< / button>
< div class =mascot>
< a class =navbar-brandhref =#>
< img src =<?php echo base_url()?> assets / img / monstercode.pngalt =Green Monster Mascotstyle =width:160px; height:32px;& < / a>
< / div>
< / div>
< div class =collapse navbar-collapseid =myNavbar>
< ul class =nav navbar-nav>
< li class =active>< a href =<?php echo site_url('')?>>首页< / 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 / employer')?>> Employers< / a>< / li>
< li>< a href =#>联系人< / a>< / li>
<?php if($ this-> session-> userdata('login')){?>
< li class =right nav navbar-nav navbar-right>< p class =navbar-text> Hello<?php echo $ this-& > userdata('uname'); ?>< / p>< / li>
< li class =right nav navbar-nav navbar-right>< a href =<?php echo base_url();?" index.php / home / logout> Log外< / a>< / li>
<?php} else {?>
< li>< a href =<?php echo base_url();?" index.php / login>登录< / a>< / li>
< li>< a href =<?php echo base_url();?" index.php / 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>使用者图片< 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>用户信息< br />名称:<?php echo $ uname; >< br />电子邮件:<?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>个人资料摘要< / h4>
< hr />
< form method =postaction =<?php echo site_url('profile / update');?>>
< p>名称:< input type =textname ='name'value =<?php echo $ uname;?>>< / p&
< p>电子邮件:< input type =textname ='email'value =<?php echo $ uemail;?>>< / p&
< button type =submitclass =btn btn-default>更新< / button>
< / form>
< form method =postaction =<?php echo site_url('profile / delete');?>>
< button type =submitclass =btn btn-default>删除< / 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>更多用户信息< / h3>
< p> Lorem ipsum ...< / p>
< / div>
< div class =col-sm-2 sidenav>
< div class =well>
< p>工作机会< br />< br />< br />< br />< br />< br / >< br />< br />< / p>
< / div>
< div class =well>
< p>工作机会< br />< br />< br />< br />< br />< br / >< br />< br />< / p>
< / div>
< / div>
< / div>
< / div>
< script type =text / javascriptsrc =<?php echo base_url(assets / js / jquery-1.10.2.js);?>> ; / script>
< script type =text / javascriptsrc =<?php echo base_url(assets / js / bootstrap.js);?>>< / script&
< / body>
< / html>
请让我知道我的代码有什么问题。
更新模型
<?php if defined('BASEPATH'))exit('不允许直接脚本访问);
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');
/ *从email = $ _Post ['email']和password = $ _post ['password']的用户中选择* /
return $ query-> result();
}
// get user
function get_user_by_id($ id)
{
$ this-> db-> where ,$ id);
//从id = $ _Post ['id']
$ query = $ this-> db-> get('user')的用户中选择ID
return $ query-> result();
}
// insert
function insert_user($ data)
{
return $ this-> db-& ,$ data);
}
//删除行
函数delete_row($ id)
{
$ this-> db->其中('id',$ id);
$ this-> db-> delete('user');
}
函数更新($ 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');
}
}
?>
解决方案
> do_upload 方法:
$ details = $ this-> user_model-> get_user_by_id this-> session-> userdata('uid'));
$ data ['uname'] = $ details [0] - > firstname。 。 $ details [0] - > lastname;
$ data ['uemail'] = $ details [0] - >电子邮件;
像下面 do_upload
方法:
$ details = $ this-> user_model-> get_user_by_id($ this-> session-> userdata('uid') );
$ data ['uname'] = $ details [0] - > firstname。 。 $ details [0] - > lastname;
$ data ['uemail'] = $ details [0] - >电子邮件;
$ data ['upload_data'] = $ this-> upload-> data();
$ data ['uid'] = $ this-> session-> userdata('uid')
$ this-> load-& $ data);
因此,现在 do_upload()
:
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] - >电子邮件;
if(!$ this-> upload-> do_upload('userfile'))
{
$ data ['error'] = $ this->上传 - > display_errors();
$ this-> load-> view('profile_view',$ data);
}
else
{
$ upload_data = $ this-> upload-> data();
$ images_name = $ upload_data ['file_name'];
//更新用户照片介绍数据库
$ 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);
}
}
Hey guys Im getting an error Undefined variable see pic http://imgur.com/oJMbrYo
What i want to do is upload an image and then send it the url to database. here is my
profile controller:
<?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'));
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';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('profile_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data(), $this->session->userdata('uid'));
$this->load->view('profile_view', $data);
}
}
`}
here is my 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>
<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>
Please let me know whats wrong with my code. Thanks.
Update Model
<?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');
}
}
?>
解决方案
Also put bellow user information in do_upload
method :
$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;
Like bellow do_upload
method :
$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['upload_data'] = $this->upload->data();
$data['uid'] = $this->session->userdata('uid')
$this->load->view('profile_view', $data);
So, now do_upload()
method is :
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);
}
}
这篇关于使用crud将图片上传到codeigniter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文