我是codeigniter的新手,并且很难理解如何在我的应用程序中使用crud。如果我使用的代码完全错误,请给我一些松懈,因为我很新,并且已经尝试了一周就已经弄明白了。
我可以创建数据并将其插入数据库,但是我无法更新。如果你们可以看一下我的代码,那就太好了。感谢。
这是我的代码:
login.php控制器
<?php
class login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form','url','html'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
$this->load->model('user_model');
}
public function index()
{
// get form input
$email = $this->input->post("email");
$password = $this->input->post("password");
$image = $this->input->post['image'];
// form validation
$this->form_validation->set_rules("email", "Email", "trim|required");
$this->form_validation->set_rules("password", "Password", "trim|required");
if ($this->form_validation->run() == FALSE)
{
// validation fail
$this->load->view('login_view');
}
else
{
// check for user credentials
$uresult = $this->user_model->get_user($email, $password, $image);
if (count($uresult) > 0)
{
// set session
$sess_data = array('login' => TRUE, 'uname' => $uresult[0]->firstname, 'uid' => $uresult[0]->id, 'image' => $uresult[0]->id);
$this->session->set_userdata($sess_data);
redirect("Profile/index");
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>');
redirect('login/index');
}
}
}
}
Profile.php 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);
}
public function upload_profile() {
$input = $this->input->post();
$config['upload_path'] = './assets/images/'; //path were I save the uploaded profile pics
$config['allowed_types'] = 'gif|jpg|png'; // allowed types that is mention
//size of the picture by default
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = true;
$this->load->library('upload', $config);
// display error if the picture is not on the config (sample bmp)
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors(); // display the errors
$data['upload_error'] = $error;
if($this->session->userdata('account_id') != null) { // if there is an account
$this->load->model('profile'); //model
$this->load->model('account'); //model
$data['user'] = $this->profile->get_profile($this->session->userdata('account_id')); //get_profile is a function in model
$data['account'] = $this->account->get_account($this->session->userdata('account_id')); //get_account is a function in model
$data['view'] = 'users/settings';
$data['title'] = "User's Settings";
$data['error'] = $error;
$this->load->view('masterpage', $data);
} else {
redirect(base_url('index.php/Profile/index'));
}
}
else
{
//if no error
$data = $this->upload->data();
$updateProfile = array(
'profile_pic' => $data['file_name']
);
$this->load->model('profile');
$this->profile->update_profile($this->session->userdata('account_id'), $updateProfile); // update the profile of the user
redirect(base_url('index.php/profile/index'));
}
}
}
user_model.php模型
<?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');
return $query->result();
}
// get user
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('user');
return $query->result();
}
// insert
function insert_user($data)
{
return $this->db->insert('user', $data);
}
public function get_profile($profile_id)
{
$this->db->select()->from('profile_tbl')->where('profile_id', $profile_id);
$query = $this->db->get();
return $query->first_row('array');
}
public function update_profile($profile_id, $data)
{
$this->db->where('profile_id', $profile_id);
$this->db->update('profile_tbl', $data);
return $this->db->affected_rows();
}
public function get_account($account_id)
{
$this->db->select()->from('account_tbl');
$this->db->where('account_id', $account_id);
$query = $this->db->get();
return $query->result_array();
}
}
?>
&GT;
Profile_view.php视图
<!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>
<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/>
<p>Name: <?php echo $uname; ?></p>
<p>Email: <?php echo $uemail; ?></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>
// default.png if haven't uploaded profile picture
<?php $profilePic = "default.png"; ?>
// if already uploaded profile picture it will display
<?php if ($user['profile_pic'] != null) { ?>
<?php $profilePic = $user['profile_pic']; ?>
<?php } ?>
// if there is an error in uploading
<?php if(isset($upload_error)) { ?>
<div class="col-lg-12">
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Uploading profile image could not be completed. <?php print_r($upload_error); ?>
</div>
</div>
<?php } ?>
<div class="col-lg-12">
//display the profile picture
<img src="<?php echo base_url('assets/images/'.$profilePic); ?>" width="100" />
// call the controller upload_profile
<?php echo form_open_multipart(base_url('index.php/Profile/upload_profile'));?>
<input type="file" name="userfile" id="userfile" size="20" style="display:none;" />
<label for="userfile" class="btn btn-info btn-sm">Choose Image</label>
<input type="submit" class="btn btn-xs" value="edit profile" />
</form>
</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>
答案 0 :(得分:0)
看看这段代码
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);
}
您正在向视图发送用户电子邮件和姓名。
但在您的视图中,您使用的是不存在的<?php $profilePic = $user['profile_pic']; ?>
。这就是您收到未定义变量错误的原因。
<强>模型强>
// instead of returning $query->result(), return $query->row() which would return only one row
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('user');
return $query->row();
}
<强>控制器强>
不是像您一样传递单个用户的详细信息,而是传递整个用户对象。您可以在此视图中显示数据Email : <?= $user->email ?>
如果您的用户表包含数百个字段,那么您不必使用数百个变量(我知道它不能包含那么多字段,只是为了指出问题)。
function index()
{
$user = $this->user_model->get_user_by_id($this->session->userdata('uid'));
$data['user'] = $user;
$this->load->view('profile_view', $data);
}
如果profile_pic
是您user
表格中的字段,则可以随时查看
<?php if($user->profile_pic != null)
答案 1 :(得分:0)
这是对你出现的评论的答案......
您的视图中的代码需要稍微更改,因为您已将PHP注释放在PHP标记之外,并且您已经过度使用了php标记,因此可以进行一些清理。你可以改变......
这
// default.png if haven't uploaded profile picture
<?php $profilePic = "default.png"; ?>
// if already uploaded profile picture it will display
<?php if ($user['profile_pic'] != null) { ?>
<?php $profilePic = $user['profile_pic']; ?>
<?php } ?>
要
<?php
// default.png if haven't uploaded profile picture
$profilePic = "default.png";
// if already uploaded profile picture it will display
if($user['profile_pic'] != null) {
$profilePic = $user['profile_pic'];
}
?>