Unable to fetch data from database in codeigniter php - php

Once the user login into site unable to fetch the data from database getting blank page if i write foreach condition here is my code.Fetching username and login verification is workig fine.
Controller:
public function index()
{
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
}
else{
$this->load->view('welcome');
}
}
Model:
function getprofiledata($id)
{
$this->db->select('profile_details.*');
$this->db->from('profile_details');
$this->db->where(array('profile_details.profile_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<form action="<?php echo base_url();?>profile/updateprofile" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<?php
echo form_hidden('profile_id',$r->profile_id);
?>
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="profilename">Profile Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="profile_name" placeholder="Enter Profile name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="designation">Designation:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="designation" placeholder="Enter Designation">
</div>
</div>
<button type="submit" class="btn">Submit</button>
</form>
<?php endforeach;endif;?>

You chose the wrong segment number on line $data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));.
Take notice that segment counting starts with zero, so segment no 3 is actually the 4th one in the uri.
If you keep the user id inside your session, you should replace $data['records']= $this->profile_model->getprofiledata($this->uri->segment(3)); with $records = $this->profile_model->getprofiledata($this->session->userdata('profile_id'))‌​‌​‌​;. And you're done.

add a new session when you login process like bellow in your login model :
<?php
public function login_user($user_name = '', $password=''){
$userdetails = array(
'email' => $user_name,
'password' => md5($password),
'status'=>1,
);
$this->db->where($userdetails);
$query = $this->db->get('profile_details');
if($query->num_rows()):
$user = $query->result();
$sess_arry = array(
'profile_id' => $user[0]->profile_id, // add new session profile_id
'first_name' => $user[0]->first_name
);
$this->session->set_userdata('admin_logged_in', $sess_arry); //add admin details to session
return true;
else:
return false;
endif;
}
?>
And some change your index method like bellow :
<?php
public function index()
{
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['country'] = $this->signup_model->getcountry();
$data['states'] = $this->profile_model->getstates();
$profile_id = $this->session->userdata('profile_id');
$records = $this->profile_model->getprofiledata($profile_id)‌​‌​;
$data['records']= $records;
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else{
$this->load->view('welcome');
}
}
?>
I have added new 3 line because i thinks you are no getting profile id properly in $this->uri->segment(3)
So,
$profile_id = $this->session->userdata('profile_id');
$records = $this->profile_model->getprofiledata($profile_id)‌​‌​;
$data['records']= $records;

Related

CI4 Inserting ID from selected Strings

I'm so tired of thinking how to solve this case. already search by internet and never solved.
straight to the point
this is my Controller of Product.php
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\Model_product;
use App\Models\Model_category;
use App\Models\Model_status;
use App\Models\Model_supplier;
class Product extends Controller
{
protected $helpers = [];
public function __construct()
{
helper(['form']);
$this->model_category = new Model_category();
$this->model_product = new Model_product();
$this->model_status = new Model_status();
$this->model_supplier = new Model_supplier();
}
public function index()
{
$data['tbproduct'] = $this->model_product->getProduct();
return view('product/index', $data);
}
public function create()
{
$all = [
'tbproduct' => $this->model_product->getProduct(),
'tbcategory' => $this->model_category->getCategory(),
'tbstatus' => $this->model_status->getStatus(),
'tbsupplier' => $this->model_supplier->getSupplier()
];
return view('product/create', $all);
}
public function store()
{
$validation = \Config\Services::validation();
$data = array(
'prod_name' => $this->request->getPost('prod_name'),
'prodcat_id' => $this->request->getGet('cat_id'),
'prod_serial' => $this->request->getPost('prod_serial'),
'prod_barcode' => $this->request->getPost('prod_barcode'),
'prodstat_id' => $this->request->getPost('stat_id'),
'prodsup_id' => $this->request->getPost('sup_id'),
'prod_image' => $this->request->getPost('prod_image')
);
if ($validation->run($data, 'product') == FALSE) {
session()->setFlashdata('inputs', $this->request->getPost());
session()->setFlashdata('errors', $validation->getErrors());
return redirect()->to(base_url('product/create'));
} else {
$model = new Model_product();
$simpan = $model->insertProduct($data);
if ($simpan) {
session()->setFlashdata('Success', 'Product has been created');
return redirect()->to(base_url('product'));
}
}
}
}
this is my Models Model_product.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class Model_product extends Model
{
protected $table = 'tbproduct';
public function getProduct($id = false)
{
if ($id === false) {
return $this->table('tbproducts')
->join('tbcategory', 'tbproduct.prodcat_id = tbcategory.cat_id', 'left')
->join('tbstatus', 'tbproduct.prodstat_id = tbstatus.stat_id', 'left')
->join('tbsupplier', 'tbproduct.prodsup_id = tbsupplier.sup_id', 'left')
->get()
->getResultArray();
} else {
return $this->table('tbproducts')
->join('tbcategory', 'tbproduct.prodcat_id = tbcategory.cat_id', 'left')
->join('tbstatus', 'tbproduct.prodstat_id = tbstatus.stat_id', 'left')
->join('tbsupplier', 'tbproduct.prodsup_id = tbsupplier.sup_id', 'left')
->where('tbproduct.prod_id', $id)
->get()
->getRowArray();
}
}
public function insertProduct($data)
{
return $this->db->table($this->table)->insert($data);
}
public function updateProduct($data, $id)
{
return $this->db->table($this->table)->update($data, ['prod_id' => $id]);
}
public function deleteProduct($id)
{
return $this->db->table($this->table)->delete(['prod_id' => $id]);
}
}
and this is my Views create.php
<?php echo view('_partials/header'); ?>
<?php echo view('_partials/sidebar'); ?>
<div class="content-wrapper">
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Create New Product</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">Create New Product</li>
</ol>
</div>
</div>
</div>
</div>
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<form action="<?php echo base_url('product/store'); ?>" method="post">
<div class="card">
<div class="card-body">
<?php
$inputs = session()->getFlashdata('inputs');
//$inputs_cat = isset($inputs['cat_id']) == null ? '' : $inputs['cat_id'];
$errors = session()->getFlashdata('errors');
if (!empty($errors)) { ?>
<div class="alert alert-danger" role="alert">
Whoops! There is something wrong, that is:
<ul>
<?php foreach ($errors as $error) : ?>
<li><?= esc($error) ?></li>
<?php endforeach ?>
</ul>
</div>
<?php } ?>
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" name="prod_name" placeholder="Enter product name" value="<?php echo isset($inputs['prod_name']); ?>">
</div>
<div class="form-row">
<div class="col mb-3">
<label for="">Category</label>
<select class="form-control" name="cat_name" id="cat_name">
<?php foreach ($tbcategory as $row) {
echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
} ?>
</select>
</div>
<div class="col mb-3">
<label for="">Serial Number</label>
<input type="text" class="form-control" name="prod_serial" placeholder="Enter product serial number" value="<?php echo isset($inputs['prod_serial']); ?>">
</div>
<div class="col mb-3">
<label for="">Barcode</label>
<input type="text" class="form-control" name="prod_barcode" placeholder="Enter product barcode" value="<?php echo isset($inputs['prod_barcode']); ?>">
</div>
</div>
<div class="form-row">
<div class="col mb-3">
<label for="">Status</label>
<select class="form-control" name="stat_name" id="stat_name">
<?php foreach ($tbstatus as $row) {
echo '<option value="' . $row['stat_id'] . '">' . $row['stat_name'] . '</option>';
} ?>
</select>
</div>
<div class="col mb-3">
<label for="">Supplier</label>
<select class="form-control" name="sup_name" id="sup_name">
<?php foreach ($tbsupplier as $row) {
echo '<option value="' . $row['sup_id'] . '">' . $row['sup_name'] . '</option>';
} ?>
</select>
</div>
</div>
</div>
<div class="card-footer">
Back
<button type="submit" class="btn btn-primary float-right">Save</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php echo view('_partials/footer'); ?>
i want to insert new data from Controller Product function store.
i've this problem, i cannot change string value into integer value for this
$data = array(
'prod_name' => $this->request->getPost('prod_name'),
'prodcat_id' => $this->request->getGet('cat_id'), ---->>> this is the problem
'prod_serial' => $this->request->getPost('prod_serial'),
'prod_barcode' => $this->request->getPost('prod_barcode'),
'prodstat_id' => $this->request->getPost('stat_id'), ---->>> and this
'prodsup_id' => $this->request->getPost('sup_id'), ---->>> and also this
'prod_image' => $this->request->getPost('prod_image')
);
if you see in the create.php Views, there is $row['***_id'] for all three tables.
and i want to grab that three ids into store.
and this is the Error shown:
mysqli_sql_exception #1048
Column 'prodcat_id' cannot be null
Your problem is that you are sending the cat_name field not the cat_id field from the form. Fix the names of the fields and will works.
The below will works
<?php
$data = array (
'prod_name' => $this->request->getPost('prod_name'),
'prodcat_id' => $this->request->getPost('cat_name'), // ---->>> name fixed **
'prod_serial' => $this->request->getPost('prod_serial'),
'prod_barcode' => $this->request->getPost('prod_barcode'),
'prodstat_id' => $this->request->getPost('stat_name'), // ---->>> and this **
'prodsup_id' => $this->request->getPost('sup_name'), // ---->>> and also this**
'prod_image' => $this->request->getPost('prod_image'),
);
Update:
Since this appears to be simple, but really this could be deceitful, i recommend the next steps to solve it:
Ensures that the form field names match with the named used on the php script
Ensure that you are sending data to the server. You can check the super globals. Something like print_r($_POST) is enough.
Ensure collect the data from the right source: get or post
In this case, the error comes from mysql. To mitigate this you can:
sets default values on columns if required
fill $data array conditionally. If there are no a cat_id, don't put an index 'cat_id' without value (or null)
thank you for helping me before.
i found the answer.
here is the correct code where i can get the cat_id, stat_id and sup_id.
in the View create.php
change this code
<select class="form-control" name="cat_name" id="cat_name">
into this
<select class="form-control" name="cat_id" id="cat_id">
this mean you will get the cat_id value from the database, instead of cat_name.
change the stat_name into stat_id, then sup_name into sup_id.
when all of that changed, you can insert the database with the correct id number when you selected the value from the dropdown box.
this is the answer of my problem, thank you for helping me.

Undefined property: App\Controllers\Admin::$user

I want to Login whenever I click the submit button, I get all information on username and password to log in and can get in into the dashboard. can somebody help me?
but I got an error like this
Undefined property: App\Controllers\Admin::$user
this is my code:
public function __construct()
{
$this->user = new UserModel();
$this->sponsor = new SponsorModel();
$this->auditorium = new auditoriumModel();
}
public function index()
{
$user = $this->user->findAll();
$audVid = $this->auditorium->findAll();
$sponsorData = $this->sponsor->findAll();
$data = [
'user' => $user,
'sponsorData' => $sponsorData,
'audvid' => $audVid
];
// dd($sponsorData);
echo view('templates/header');
echo view('login', $data);
echo view('templates/footer');
}
public function doLogin()
{
$email
= $this->request->getVar('email');
$password =
$this->request->getVar('password');
$userData = $this->user->where('email', $email)
->where('password', $password)
->findAll();
if ($userData == null) {
return redirect()->to('/admin');
} else {
$_SESSION['logonUser'] = 'aktif';
return redirect()->to('/adminDashboard', $userData);
}
}
}
this is my login view
<form id="login-form" class="form" action="/admin/doLogin" method="post">
<h3 class="text-center text-info">Login Admin</h3>
<div class="form-group">
<label for="username" class="text-info">Username:</label><br>
<input type="text" name="username" id="username" class="form-control">
</div>
<div class="form-group">
<label for="password" class="text-info">Password:</label><br>
<input type="text" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<labezl for="remember-me" class="text-info"><span>Remember me</span> <span><input id="remember-me" name="remember-me" type="checkbox"></span></labezl><br>
<input type="submit" class="btn btn-info btn-md" value="submit" href="/adminDashboard">
</div>
<div id="register-link" class="text-right">
Register here
</div>
</form>
can somebody help me to solve my code?
thanks
$userData = $this->user->where('email', $email)
->where('password', $password)
->findAll();
findAll() will get you an array of rows that applies to your criteria. In your case will try to find all users where email and password are correct, but it will still return you an array of them, not just one. You need to be using find() instead.

Create Admin LoginPage With Codeigniter

I'm trying to create some admin login. I think my code is correct because when I input wrong email and password the error message appears on login page. However, when I input correct data of my DB, it also shows error message.
Please help me. I appreciate any answer.
my controller (Admin.php):
public function index()
{
$this->admindashboard();
}
public function admindashboard()
{
$data = array();
/* $data['main_content'] = $this->load->view('admin_main', '', TRUE); */
$this->load->view('admin/admin_main', $data);
}
public function admin_regst()
{
$data = array();
$data['main_content'] = $this->load->view('admin-regist', '', TRUE);
$this->load->view('admin/admin_main', $data);
}
My other controller (Login_admin.php):
public function index()
{
$this->load->view('login');
}
public function adminchecklogin()
{
$data = array();
$adminemail = $this->input->post('admin_email', TRUE);
$adminpassword = $this->input->post('admin_psw', TRUE);
$this->load->model('M_login_admin');
$admindetails = $this->M_login_admin->admin_login_check($adminemail);
if (password_verify($adminpassword, $admindetails->admin_psw)) {
if ($admindetails->admin_status == 1) {
$session_data['adminid'] = $admindetails->admin_id;
$session_data['adminemail'] = $admindetails->admin_email;
$session_data['adminusername'] = $admindetails->admin_username;
$session_data['adminstatus'] = $admindetails->admin_status;
$this->session->set_userdata($session_data);
redirect('Admin');
} else {
$data['error_msg'] = "User ini tidak aktif....!!!";
redirect('login', $data);
}
} else {
redirect('error-login', $data);
}
}
public function login_error()
{
$data['error_msg'] = "Email atau Password Anda Salah....!!!";
$this->load->view('login', $data);
}
my model (M_login_admin.php):
public function admin_login_check($adminemail)
{
$admin_details = $this->db->select('*')
->from('admin')
->where('admin_email', $adminemail)
->get()
->row();
return $admin_details;
}
my view(login.php):
<body>
<section class="hero is-fullheight">
<div class="hero-body container has-text-centered">
<div class="login">
<img src="https://logoipsum.com/logo/logo-1.svg" width="325px" />
<p>
<?php
if (isset($success_msg)) {
echo $success_msg;
}
?>
</p>
<p>
<?php
if (isset($error_msg)) {
echo $error_msg;
}
?>
</p>
<form action="<?= base_url(); ?>Login_admin/adminchecklogin" method="POST">
<div class="box">
<div class="field">
<div class="control">
<input class="input is-medium is-rounded" type="email" placeholder="hello#example.com" autocomplete="username" name="admin_email" required />
</div>
</div>
<div class="field">
<div class="control">
<input class="input is-medium is-rounded" type="password" placeholder="**********" autocomplete="current-password" name="admin_psw" required />
</div>
</div>
<div class="field has-text-left ml-3 mt-5">
<label class="checkbox">
<input type="checkbox">
Remember me
</label>
</div>
</div>
<button class="button is-block is-fullwidth is-primary is-medium is-rounded" type="submit">
Login
</button>
</form>
<br>
</div>
</div>
</section>
My route
DB Column
DB Data
Interface
Your database has the admin_psw in plain text that would explain why the password_verify() is failing.
In order to use password_verify(), you have to have hashed the password using password_hash() This would normally be done when the user first registers or any time the user changes their password
Check the documentation for password_hash() in the PHP manual

Codeigniter Login Issues

I'm having difficulties in understanding what's going on here.
I've made a login page, setting it to go to admin page. Without any user data yet, just to check if it's working. And It doesn't go. I hade some problems loading the library form_validation. So I added the parent construct.
Controller
class Login Extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('login');
$this->load->helper('url');
}
public function login() {
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata($user_session);
redirect('login/admin');
}
}
public function admin() {
$this->load->view('admin');
}
}
My Login View
<section class="login_content">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url().'login/login'; ?>" method="post">
<h1>Login no Sistema</h1>
<div>
<input type="text" name="username" class="form-control" placeholder="Username" required="" />
</div>
<div>
<input type="password" name="password" class="form-control" placeholder="Password" required="" />
</div>
<div>
<input type="submit" name="submit" value="Login" />
</div>
<div class="clearfix"></div>
<div class="separator">
<div class="clearfix"></div>
<br />
<div>
<h1> Sitio Monica e Marcia</h1>
<p>©2016 Todos os direitos reservados. Sitio Monica e Marcia.</p>
</div>
</div>
</form>
<!-- form -->
</section>
My Admin View
<div id="login" class="animate form">
<section class="login_content">
<h1>Bem vindo ao Admin</h1>
<?php
echo '<pre>';
print_r($this->session->all_userdata());
echo '<pre>';
?>
<!-- form -->
</section>
<!-- content -->
</div>
Here Is Your Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Description: Login controller class*/
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
}
public function admin($msg = NULL){
// Load our view to be displayed
// to the user
$data['msg'] = $msg;
if($msg == NULL)
{
$this->load->view('login');
}
else
{
//print_r($data);
//die();
$this->load->view('login',$data);
}
}
public function process(){
// Load the model
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = 'Invalid username or password';
$this->admin($msg);
}else{
// If user did validate,
// Send them to members area
redirect('home/check_isvalidated');
}
}
public function doLogout(){
$this->session->sess_destroy();
redirect(base_url());
}
}
Here IS The Model You Can Use Validation instead of Security
public function validate(){
// grab user input
$username = $this->security->xss_clean($this->input->post('user_name'));
$password = $this->security->xss_clean(md5($this->input->post('password')));
// Prep the query
$this->db->where('user_name', $username);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('admin');
// Let's check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'id' => $row->id,
'user_name' => $row->user_name,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
else {
return false;
}
}
}
Try this
redirect(base_url('login/admin'));
I think, you should check your link and session. This is the code
login.php in controller
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function index() {
//$this->load->view('login');
$this->load->view('login');
}
public function login() {
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata('userlogin',$user_session);
$this->admin();
}
}
public function admin() {
$this->load->view('admin');
}
login.php in view
<form class="form-horizontal" role="form" action="<?php echo 'http://localhost/answers/index.php/login/login'; ?>" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="username">Username:</label>
<div class="col-sm-10">
<input type="username" class="form-control" id="username" name="username" placeholder="Enter username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>
And admin.php in views
<div class="container" height=100%>
<h2>Welcome to the system,
<?php
$this->load->library('session');
$login_session = $this->session->userdata('userlogin');
//$username = $this->session->userdata('userlogin');
echo $login_session['Username'];
?>
</h2>
</div>
May you want to see the complete code like http://explicitphp.blogspot.co.id/2016/03/Codeigniter-Login-System-Tutorial-Without-Database-Connection.html
Hope answer your question. Have fun guys
first, i don't know if the Login.php file or the class Login extends CI_Controller is a reserved name, try changing it.
then you have a public function called login(), this can have problems with the constructor, because in php5 or above you call the constructor like the class name and you maybe override the constructor

how update value input form class result

I Have a profile page and admin can edit profile users,
all process on one page done,How i can refresh and update value form data after success query update ?
User.class file :
class User {
...
public function updateUser($id, $firstname, $lastname, $phone, $birthday, $managerid)
{
$con = $this->DBconnect();
$id = (int)$id;
$managerid = $this->checkParam($managerid);
$firstname = $firstname;
$lastname = $lastname;;
$mobile = $phone;
$birthday = $this->checkParam($birthday);
$query = "UPDATE `users` SET `manager_id` = :manager_id,`firstname` = :firstname,`lastname` = :lastname,`birthday` = :birthday,`mobile` = :mobile WHERE `id` = :id";
$result = $con->prepare($query);
$result->BindParam(':id', $id, PDO::PARAM_INT);
$result->BindParam(':manager_id', $managerid, PDO::PARAM_INT);
$result->BindParam(':firstname', $firstname);
$result->BindParam(':lastname', $lastname);
$result->BindParam(':birthday', $birthday);
$result->BindParam(':mobile', $mobile);
$check = $result->execute();
return true;
}}
profile.php file :
<?php
if (isset($_GET['id'])) {
$id = (int)$_GET['id'];
}
$user = new User();
$user_info = $user->getuser($id);
while ($info = $user_info->fetch(PDO::FETCH_ASSOC)) {
$firstname = $info['firstname'];
$lastname = $info['lastname'];
$mobile = $info['mobile'];
$birthday = $info['birthday'];
$managerid = $info['manager_id'];
}
$manager_ob = new Manager();
$managers = $manager_ob->getAllManager();
$managers_name = array();
while ($manager = $managers->fetch(PDO::FETCH_ASSOC)) {
$managers_list[] = $manager;
}
if (isset($_POST['edit-profile'])) {
$update_result = $user->updateUser($_POST['user_id'],$_POST['user_firstname'],$_POST['user_lastname'],$_POST['user_mobile'],$_POST['user_birthday'],$_POST['manager_id']);
if($update_result){
echo 'Profile Edited';
}
}
?>
<form method="post" action="#" class="form-horizontal">
<div class="form-group"><label class="col-sm-2 control-label">ID</label>
<div class="col-sm-10"><input type="text" readonly class="form-control" name="user_id" id="user_id" value="<?php echo check_param($id); ?>"/></div>
</div>
<div class="form-group"><label class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10"><input type="text" class="form-control" name="user_firstname" value="<?php echo check_param($firstname); ?>" /></div>
</div>
<div class="form-group"><label class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10"><input type="text" class="form-control" name="user_lastname" value="<?php echo check_param($lastname); ?>"/></div>
</div>
<div class="form-group"><label class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10"><input type="text" class="form-control" name="user_mobile" value="<?php echo check_param($mobile); ?>"/></div>
</div>
<div class="form-group"><label class="col-sm-2 control-label" for="birthday">Birthday
</label>
<div class="col-sm-10"><input id="birthday" type="text" class="form-control" name="user_birthday"></div>
</div>
<div class="form-group"><label class="col-sm-2 control-label">Manager</label>
<div class="col-sm-10"><select class="form-control m-b" name="manager_id">
<?php foreach ($managers_list as $managers_n) { ?>
<option <?php if ($managers_n['id'] == $managerid) {
echo 'selected';
} ?>
value="<?php echo $managers_n['id']; ?>"> <?php echo $managers_n['name']; ?></option>;
<?php }
?>
</select>
</div>
</div>
<input type="submit" name="edit-profile" class="btn btn-block btn-w-m btn-success"
value="Edit profile">
</form>
i load profile data after submit edit :
$update_result = $user->updateUser($_POST['user_id'],$_POST['user_firstname'],$_POST['user_lastname'],$_POST['user_mobile'],$_POST['user_birthday'],$_POST['manager_id']);
if($update_result){
echo 'Profile Edited';
}
only display message Profile Edited but must be refresh page for renew data
I must fetch again query for update values? or have better way ?
I suggest you use Ajax for this this is probably the best way to change the data without refreshing. More info about (jQuery) ajax http://api.jquery.com/jquery.ajax/
Your other option is to force a refresh after the submit. You can do this in PHP like so:
Header('Location: '.$_SERVER['PHP_SELF']);
I would suggest choosing ajax to tackle this problem though.
Good luck :)

Categories