i can't update database using code igniter - php

i'm using controller to call update function from model, but when i try to update for column name, that's no working.
---- Controller ----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class editprofile extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function index()
{
$this->form_validation->set_rules('email', 'email', 'required|valid_email');
$this->form_validation->set_rules('namalengkap', 'namalengkap', 'required');
$this->form_validation->set_rules('password', 'PASSWORD', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('v_profile');
} else {
$row = $this->session->userdata('id');
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'kecamatan' => $this->input->post('kecamatan'),
'kelurahan' => $this->input->post('kelurahan'),
'rt' => $this->input->post('rt'),
'rw' => $this->input->post('rw'),
'zip code' => $this->input->post('zip code'),
'city' => $this->input->post('city'),
'province' => $this->input->post('province'),
);
$result = $this->user->update($data, $row);
if ($result > 0) {
$this->updateProfil();
$this->session->set_flashdata('msg', show_succ_msg('Data Profile Berhasil diubah, silakan lakukan login ulang!'));
redirect('c_login/profile');
} else {
$this->session->set_flashdata('msg', show_err_msg('Data Profile Gagal diubah'));
redirect('c_login/profile');
}
}
}
}
----- model -----
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model
{
private $uid = 'id';
private $tabel = 'users';
public function __construct()
{
parent::__construct();
}
public function login($email, $password)
{
$this->db->where('email', $email);
$this->db->where('password', $password);
return $this->db->get($this->tabel);
}
function update($data, $id)
{
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}
public function get_by_cookie($kue)
{
$this->db->where('cookie', $kue);
return $this->db->get($this->tabel);
}
}
----- Core / MY_Controller -----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function updateProfil()
{
if ($this->userdata != '') {
$data = $this->user->select($this->userdata->email);
$this->session->set_userdata('userdata', $data);
$this->userdata = $this->session->userdata('userdata');
}
}
}

In your update function you have not set any values. As set method required array values you should give if. Good practice is using set before update and finally update of table. Follow the following codes for updating your users table.
function update($data, $id)
{
$this->db->set($data);
$this->db->where($this->uid, $id);
$this->db->update($this->tabel);
}
use this code. I hope it will work well.

in your controller
$result = $this->user->update($data, $row);
variable data is array
so in your model:
function update($data=array(), $id){
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}

Related

Fail to update the fields in the database in codeigniter

I'm trying to create the function to update user details. but the database transaction in the model fails. It just pops out failure message without giving any db errors or anything.
Is there a way to view db logs for debugging in model like var_dump in controller
controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class edit_account extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
$this->load->model('medit_account');
}
function index() {
}
function edit_dept_officer() {
$user_id = $this->session->userdata('user_id');
// retrieve data from db to edit from
$table = 'department_officer';
$user_data = $this->medit_account->get_current_user($table, $user_id);
var_dump($user_data);
$userid = $user_data[0]->OfficerID;
$data = array(
'officer_no' => $user_data[0]->officer_no,
'first_name' => $user_data[0]->first_name,
'last_name' => $user_data[0]->last_name,
'contact_no' => $user_data[0]->contact_no,
);
var_dump($userid);
//validation rules
// $this->form_validation->set_rules('officer_no', 'Officer ID ', 'trim|required');
$this->form_validation->set_rules('first_name', 'First Name ', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name ', 'trim|required');
$this->form_validation->set_rules('contact_no', 'Contact Number', 'trim|required|numeric');
var_dump($data);
if ($this->form_validation->run() == FALSE) {
//fail validation
// $this->load->view('header');
$this->load->view('edit_account_dept_view', $data);
$this->load->view('footer');
// var_dump('point');
} else {
// pass validation
$officer_no = $user_data[0]->officer_no;
$first_name = $this->input->post('first_name', TRUE);
$last_name = $this->input->post('last_name' , TRUE);
$contact_no = $this->input->post('contact_no', TRUE);
$array1 = array(
'officer_no' => $officer_no,
'first_name' => $first_name,
'last_name' => $last_name,
'contact_no' => $contact_no,
);
var_dump($array1);
echo 'braekpoint';
$tableid = 'OfficerID';
var_dump($userid);
var_dump($tableid);
var_dump($table);
if($this->medit_account->update_user( $userid, $array1)) {
// user creation ok
echo 'success';
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Department Officer is created !!!</div>');
// $this->load->view('header');
$this->load->view('edit_account_dept_view', $data);
$this->load->view('footer');
// redirect('edit_account');
} else {
// user creation failed
$this->session->set_flashdata('msg', '<div class="alert alert-warning text-center">There was a problem creating the Department Officer. Please try again.!!!</div>');
echo 'fail';
// send error to the view
$this->load->view('header');
$this->load->view('edit_account_dept_view', $data);
$this->load->view('footer');
}
}
}
model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class medit_account extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function get_current_user($table,$user_id) {
$this->db->select('*');
$this->db->from($table);
$this->db->where('LoginId', $user_id);
$query = $this->db->get();
$user_data = $query->result();
return $user_data;
}
public function update_user( $userid, $array1) {
$this->db->trans_start();
$this->db->where('OfficerID', $userid);
$this->db->update('department_officer', $array1);
return ;
}
}
?>

Undefined variable: result in codeigniter

I am trying to get to data from database details from model to controller i was not get
it's saying Undefined variable: result can any one know how to get data from model to controller?
controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_model');
$this->load->helper(array('form', 'url'));
$this->load->library('session');
}
public function setting()
{
$this->load->database();
session_start();
$userss="";
if($_SESSION["registerid"])
{
$userss = $_SESSION["registerid"];
}
$this->load->model('setting_model');
$this->setting_model->getsetting($userss);
$data12345 = array(
'registerid' => $result[0]->registerid,
'username' => $result[1]->username,
'email' => $result[2]->email,
'password' => $result[3]->password,
'companyname' => $result[0]->companyname,
'phonenumber' => $result[0]->phonenumber ,
'address' => $result[0]->address,
'postalcode' => $result[0]->postalcode,
'descriptionofcompany' =>$result[0]->descriptionofcompany,
'creditcardinformation' =>$result[0]->creditcardinformation,
'title'=>'Setting Page'
);
//passing page title
$this->load->helper('url'); // passing base url
$this->load->view('setting',$data12345); // passing setting.php view
}
}
setting_model.php
<?php
class setting_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getsetting($userss) {
$this->load->database();
$condition = "username =" . "'" . $userss . "'";
$this->db->select('*');
$this->db->from('register');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
// Read data from database to show data in admin page
}//end class
?>
Looks like you forgot to assign the query result to the $result variable. Try this $result = $this->setting_model->getsetting($userss); in the controller.

Codeigniter show extra data by privilege

Okay, I have used someones CodeIgniters Register/Login form. But i have a menu with projects where some options should only be shown if your privilege == 1. i took the existing database and added a privilege column, when you make a new user this value is 0, if your admin this will be changed to 1.
My question:
I have this menu
<div class="menu_rechts">
<ul>
<?php
error_reporting(-1);
ini_set('display_errors',1);
$this->load->model('user_model');
if($this->session->userdata('privilege')== "1") {
echo "test";
} else {
echo "test2";?>
<li>Login/Register</li>
</ul>
</div>
It gets included in a header
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
if($this->session->userdata('logged_in')=== TRUE)
{
$this->welcome();
}
else{
$data['title']= 'Home';
$this->load->view('header',$data);
$this->load->view("registration.php", $data);
$this->load->view('footer',$data);
}
}
public function welcome()
{
$data['title']= 'Welcome';
$this->load->view('header',$data);
$this->load->view('welcome.php', $data);
$this->load->view('footer',$data);
}
public function login()
{
$email=$this->input->post('email');
$password=md5($this->input->post('pass'));
$result=$this->user_model->login($email,$password);
if($result) $this->welcome();
else $this->index();
}
public function thank()
{
$data['title']= 'Thank';
$this->load->view('header',$data);
$this->load->view('thank.php', $data);
$this->load->view('footer',$data);
}
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->user_model->add_user();
$this->thank();
}
}
public function logout()
{
$newdata = array(
'user_id' =>'',
'user_name' =>'',
'user_email' => '',
'privilege' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
$this->session->sess_destroy();
$this->index();
}
}
?>
And finally the model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function login($email,$password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("user");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows->id,
'user_name' => $rows->username,
'user_email' => $rows->email,
'privilege' => $rows->privilege,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
public function add_user()
{
$data=array(
'username'=>$this->input->post('user_name'),
'email'=>$this->input->post('email_address'),
'password'=>md5($this->input->post('password'))
);
$this->db->insert('user',$data);
}
}
?>
i want to echo test, only if the privilege of the logged in user == 1

Codeigniter: Unable to differentiate between admin and member page

This is the login controller code:
public function login_validation(){
$this->load->library('form_validation');
$this->load->model('model_users');
$this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|xss_clean|strip_tags');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim|xss_clean|strip_tags');
if ($this->form_validation->run()){
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
if($this->model_users->get_status($data['username'])){
$data = array(
'username' => $this->input->post('username'),
'status' => 'member',
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('member');
} else {
$data = array(
'username' => $this->input->post('username'),
'status' => 'admin',
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('admin');
}
} else {
$this->login();
}
}
This is the admin controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
if (! $this->session->userdata('is_logged_in')){
$status = $this->session->userdata('status');
if ($status == 'member'){
redirect('main/restricted');
}
redirect('main/restricted');
}
}
public function index() {
$data = array(
'title' => 'Admin Page'
);
$this->load->view("header", $data);
$this->load->view("admin");
$this->load->view("nav");
$this->load->view("footer");
}
}
This is the member controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Member extends CI_Controller {
public function __construct(){
parent::__construct();
if (! $this->session->userdata('is_logged_in')){
$status = $this->session->userdata('status');
if ($status == 'admin'){
redirect('main/restricted');
}
redirect('main/restricted');
}
}
public function index() {
$data = array(
'title' => 'Member Page'
);
$this->load->view("header", $data);
$this->load->view("member");
$this->load->view("nav");
$this->load->view("footer");
}
}
I am wondering why after the member login, they are able to go to the admin page, and also after the admin have logged in they are also able to access the member page.
I would want it to work as, after the member log in the member will only be able to access the member page. After the admin log in the admin will only be able to access the admin page.
Can someone help me please.
if (! $this->session->userdata('is_logged_in'))
I think your if condition returns false.
You did not write anything for elsecondition.Write some code for else.
If your if condition is false everyone can have access for member and admin controller.
Make sure your if condition returns true
i think your code may be like this
if (! $this->session->userdata('is_logged_in'))
{
//write code for not loged user
}
else
{
$status = $this->session->userdata('status');
if ($status == 'member'){
redirect('main/restricted');
}
}
You need to compare session status with 'member' or 'admin' instead of just checking if it is set.
In Admin controller:
$status = $this->session->userdata('status')
if ($status == 'member'){
redirect('main/restricted');
}
and similarly compare with 'admin' in member controller.

Where to find table in codeigniter?

I am new with codeigniter so this feels like a silly question but I have been looking all over. I have created a log in and account registration page using codeigniter. After submitting a form, I get this:
Error Number: 1146
Table 'users.user' doesn't exist
INSERT INTO `user` (`email`, `firstname`, `lastname`, `username`, `password`, `hint`) VALUES ('blah', 'blah', 'blah', 'blah', 'fa348efcd3bb1a1fc6ba5c2c912cf402', 'Brown')
Filename: F:\htdocs\system\database\DB_driver.php
Line Number: 330
The problem is that the table is called "users" not user. My question is how do I find the table name so I can change it to "users" instead of "User".
Here is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
if(($this->session->userdata('username')!=""))
{
$this->welcome();
}
else{
$data['title']= 'Home';
$this->load->view('header_view',$data);
$this->load->view("registration_view.php", $data);
$this->load->view('footer_view',$data);
}
}
public function welcome()
{
$data['title']= 'Welcome';
$this->load->view('header_view',$data);
$this->load->view('welcome_view.php', $data);
$this->load->view('footer_view',$data);
}
public function login()
{
$email=$this->input->post('email');
$password=md5($this->input->post('pass'));
$result=$this->user_model->login($email,$password);
if($result) $this->welcome();
else $this->index();
}
public function thank()
{
$data['title']= 'Thank';
$this->load->view('header_view',$data);
$this->load->view('thank_view.php', $data);
$this->load->view('footer_view',$data);
}
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[3]|max_length[32]');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|min_length[2]|max_length[32]');
$this->form_validation->set_rules('hint', 'hint', 'trim|required|min_length[2]|max_length[32]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->user_model->add_user();
$this->thank();
}
}
public function logout()
{
$newdata = array(
'user_id' =>'',
'username' =>'',
'user_email' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
$this->session->sess_destroy();
$this->index();
}
}
?>
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function login($email,$password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("users");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows->id,
'user_name' => $rows->username,
'user_email' => $rows->email,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
public function add_user()
{
$data=array(
'email'=>$this->input->post('email'),
'firstname'=>$this->input->post('firstname'),
'lastname'=>$this->input->post('lastname'),
'username'=>$this->input->post('username'),
'password'=>md5($this->input->post('password')),
'hint'=>$this->input->post('hint')
);
$this->db->insert('user',$data);
}
}
?>
Your insert method is trying to insert into the table user. Change user to users
public function add_user()
{
$data=array(
'email'=>$this->input->post('email'),
'firstname'=>$this->input->post('firstname'),
'lastname'=>$this->input->post('lastname'),
'username'=>$this->input->post('username'),
'password'=>md5($this->input->post('password')),
'hint'=>$this->input->post('hint')
);
$this->db->insert('user',$data);
}
Your code written
$query=$this->db->get("users");
change to user
*you should have to learn about the writting style of CI.

Categories