这是edit-profile1.php 查看:
<body>
<?php include("header.php"); ?>
<?php include_once("analyticstracking.php") ?>
<div class="container-fluid hero1" style="margin-top:-20px";>
<div class="hero-inner">
<h2 class="white">My Profile</h2>
</div>
</div>
<div class="container" style="margin-top: 25px;">
<h1><b>ACCOUNT</b></h1>
<h3 style="padding-bottom: 50px;"><?php
date_default_timezone_set("Asia/Kuala_Lumpur");
$hour=date("H");
if ($hour <=6) {
echo "Welcome, ";
}else if ($hour<=12){
echo "Good morning, ";
}else if($hour <=18){
echo "Good afternoon, ";
}else if($hour >=18){
echo "Good evening, ";
}else{
echo "Hello, ";
}
?>
<?php echo ucfirst($this->session->userdata('username')); ?></h3>
<div class="row padd">
<label class="label">Phone No.</label></br>
<p class="friendly"><?php echo $userdata[0]['phone1']; ?></p><br>
</div>
<div class="row padd">
<label class="label">Email</label><br>
<p class="friendly"><?php echo $userdata[0]['email'];?></p><br>
</div>
<form action="<?php echo base_url(); ?>users/update/" method="post" enctype="multipart/form-data" data-toggle="validator">
<div class="padd col-xs-12 col-md-3">
<label class="label">My IC/Matric Card</label></br>
<input name="image" placeholder="Upload" class="upload friendly" type="file" accept="image/*" capture="environment" required>
</div>
<div class="col-xs-12 col-md-9 padd" style="overflow:auto;">
<div class="picture"><img id="blah" style="width: 100%; height: 100%"
<?php
if($userdata[0]['image'] == "")
{
echo "src='https://style.anu.edu.au/_anu/4/images/placeholders/person.png'";
}else{
echo "src='" . str_replace('/index.php/','/',base_url())."media/images/users/".$userdata[0]['image']."'";
}
?> >
</div>
</div>
<div class="row padd">
<button type="submit" style='width: 100%; margin-top:20px; margin-bottom: 20px !important; background-color: #59d389; color: white;' class="btn btn-responsive btn-md center-block">Save Changes</button>
<input type="hidden" value="<?php echo $userdata[0]['id']; ?>" name="id" />
<input type="hidden" value="<?php echo $userdata[0]['account_status']; ?>" name="id" />
<input type="hidden" value="<?php echo $userdata[0]['image']; ?>" name="oldimage"/>
</div>
</form>
</body>
</div>
<?php include 'footer.php' ?>
<script type="text/javascript">
$(document).on('change','.upload',function(){
files = this.files;
size = files[0].size;
if( size > 2000000){
alert('Please upload an image less than 2mb');
return false;
}
readURL(this);
return true;
});
</script>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width(300)
.height(150);
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
这是Users.php 控制器:
public function profile()
{
$data['userdata'] = $this->usermodel->get_user($this->session->userdata('id'));
$this->load->view('edit-profile1',$data);
}
public function update()
{
$user = new Users_model();
// $user->phone1 = $this->input->post('phone1');
$user->account_status = 1;
//session data
$user->id = $this->session->userdata('id');
$user->password = $this->session->userdata('password');
$user->username = $this->session->userdata('username');
$user->email = $this->session->userdata('email');
$user->user_role = $this->session->userdata('user_role');
$image_type=$_FILES['image']['type'];
$image_type = str_replace("image/", ".", $image_type);
$image_name=$user->id . $image_type;
//id to be set
$id = $user->id;
$config['upload_path'] = 'media/images/users';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['overwrite'] = TRUE;
$config['file_name'] = $image_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image')) {
$this->upload->display_errors('<span>', '</span>');
$data['error'] = $this->upload->display_errors();
$user->image = $this->input->post('oldimage');
} else {
$file_data = $this->upload->data();
$data['success'] = "Your file is uploaded";
$this->usermodel->update_image($user->id, $image_name);
$user->image = $image_name;
}
$res = $this->db->update('users', $user, array('id' => $id));
$URL = base_url() . "users/profile/";
redirect($URL, "refresh");
}
以下是Users_model.php 模型:
<?php
class Users_model extends CI_Model{
function __construct(){
parent::__construct();
}
public $id="";
public $username="";
public $email="";
public $password="";
public $image="";
// public $description="";
public $firstname="";
public $lastname="";
public $gender="";
public $phone1="";
// public $phone2="";
public $birthdate="";
public $cnic="";
public $address="";
public $licsense_status="";
public $expire_date="";
public $licsense_no="";
public $full_address="";
public $account_status=0;
public $user_role="";
//GET USER DATA
function login($user){
$conditions = array(
"email" => $user->email,
// "password" => $user->password,
"user_role" => $user->user_role,
"account_status" => 1,
);
$this->db->select('*');
$this->db->from('users');
$this->db->where($conditions);
$rs= $this->db->get();
$result_array = $rs->row_array();
if(password_verify($user->password,$result_array['password'])){
return $result_array;
}else{
$url = base_url() . "login?login=failed";
redirect($url, "refresh");
}
}
//GET USER DATA
function get_user($id){
$sql = "SELECT * FROM users where id=".$id;
$rs = $this->db->query($sql)->result_array();
return $rs;
}
//insert new user
function insert($user)
{
$conditions = array(
"email" => $user->email,
);
$this->db->select('*');
$this->db->from('users');
$this->db->where($conditions);
$rs= $this->db->get();
$result_array = $rs->row_array();
if($user->email == $result_array['email']){
$url = base_url() . "signup?signup=failed";
redirect($url, "refresh");
}else{
$this->db->insert('users', $user);
return $this->db->insert_id();
}
}
//update password
function update_password($user)
{
$data = array(
"password" => $user->password
);
$conditions = array(
"id" => $user->id
);
$res = $this->db->update('users', $data, $conditions);
return $res;
}
public function upload_user_image($image_name) {
$config['upload_path'] = 'media/images/users';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['overwrite'] = TRUE;
$config['file_name'] = $image_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image')) {
$this->upload->display_errors('<span>', '</span>');
$data['error'] = $this->upload->display_errors();
} else {
$file_data = $this->upload->data();
$data['success'] = "Your file is uploaded";
}
}
public function update_image($id,$image_name){
$sql = "update users set image = '$image_name' where id = $id ";
$this->db->query($sql);
}
}
我只在页面上显示电话,电子邮件和图片,users
无法更改电子邮件和电话号码但每当我提交表单时,电话号码会自动消失,我必须在数据库中手动添加它们。