Where to find table in codeigniter? - php

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.

Related

i can't update database using code igniter

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);
}

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 ;
}
}
?>

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

To change password in mysql

In the below codeigniter code i have placed controller and model i can login by entering username and password.Now i have to implement is to change password.Pls anyone help me to solve the issue.
Controller:
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site1/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
$this->load->helper('date');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
?>
model:
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'date' => date('Y-m-d H:i:s', now()),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
if ($this->db->_error_number() == 1062)
{
echo"<script>alert('This value already exists');</script>";
}
if ($this->db->_error_number() == "")
{
$this->session->set_flashdata('create', 'create');
}
return $insert;
}
function curdate() {
// gets current timestamp
date_default_timezone_set('Asia/Manila'); // What timezone you want to be the default value for your current date.
return date('Y-m-d H:i:s');
}
}
You can follow these simple steps:
Create a view with the change password form (i.e. email, current password and new password)
Create in your controller a function that retrieve the data from the form and check if exists user with that email and that password
Create a function in your model to make the update with $this->db->update()
From your controller run the model function

Fatal error: Call to a member function where() on a non-object in

I've been debugging this code but still not successful. Can anyone help me out please?
class Membership_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
I kept on getting an fatal error on the line
$this->db->where('username',$this->input->post('username'));
this is the controller/login.php
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
The database probably is not being initialized properly before validate is called.
Looks like you're not connected to your DB. Make sure you are connecting to your database.
You can either connect automatically everytime your script runs or connect to the DB manually. Look at the CI guides for your connection options : http://codeigniter.com/user_guide/database/connecting.html
Could it be that the load->model( 'membership_model' ) should receive the name of a class, and that that name is case sensitive? You should probably check the return value of the API functions...

Categories