Working user manage page with codeigniter, I made edit user function my self and that works but notification can't working. Mean If problem in updating user detail that should through error or Success but nothing happening but datas are updating.
Here is Model(Admin_model.php):
public function editUser($id, $data)
{
$this->db->where('id', $id);
$this->db->update('users', $data);
}
controller(Admin.php):
public function edit_user($id)
{
//set validation rules
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('default/admin/edit_user');
} else {
$data = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'email' => $this->input->post('email'),
'updated_at' => date('Y-m-j H:i:s')
);
if($this->Admin_model->editUser($id,$data)) {
$this->session->set_flashdata('global_alert','<div class="alert alert-success text-center">User updated!</div>');
redirect('admin/users');
} else {
$this->load->view('default/admin/edit_user');
$this->session->set_flashdata('global_alert','<div class="alert alert-danger text-center">Something wrong!</div>');
}
}
}
$this->Admin_model->editUser is not returning anything. try
public function editUser($id, $data)
{
$this->db->where('id', $id);
$result = $this->db->update('users', $data);
if ($result === FALSE)
{
show_error('error !');
}
return $result;
}
Related
I have a registration system with CodeIgniter but currently I have no control on email and password. A user can register without putting email or a password.
I have an index() and a register_user function() in my Signup controller but the redirection is not working on success
At the moment I have the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->load->view('home-view');
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
function register_user(){
$this->load->library('custom_u_id');
$data = array('user_id' => $this->custom_u_id->construct_id('USR'),
'name' => $_POST['name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
);
$this->load->model('signup_model');
$user_details = $this->signup_model->register_user($data);
if (!empty($user_details)){
$user_data = array
(
'user_id' => $user_details['user_id'],
'email' => $user_details['email'],
'name' => $user_details['name'],
'user_type' => $user_details['user_type'],
);
$this->session->set_userdata('sessiondata',$user_data);
if (intval($user_details['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
} else{
redirect('login');
}
}// end of function login
}
Do I need to put the form_validation in my register_user function ? I've tried but the check doesn't work anymore...
I also have in my view the <?php validation_errors();?> function and the <?php form_open(base_url().'signup');?>
looking by your code, i think you want to put register_user() inside validation TRUE since the query is in that method.
so try to change your code to this :
public function index()
{
if(!isset($this->session->userdata['sessiondata']['user_id']))
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('signup-view');
}
else
{
$this->register_user();
}
}else{
if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
redirect(base_url().'admin');
} else {
redirect(base_url().'home');
}
}
}
and be sure your form action to like this :
<form action="<?=site_url('/signup/index');?>">
# table name is users#
## model name is user_model##
### controller name is get_password ###
issue - no change on the password , remain as old
> model(user_model)
public function updatePassword($email,$data)
{
$data1=array('password'=>$data);
$this->db->where('email','$email');
$this->db->update('users','password');
$success = $this->db->affected_rows();
if(!$success){
error_log('Unable to updatePassword');
return false;
}
return true;
}
> controller(get_password)
public function index($rs=FALSE)
{
$this->load->database();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->library('session');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('passconf', 'password Confirmation', 'required|matches[password]');
if ($this->form_validation->run() == FALSE)
{
echo form_open();
$this->load->view('users/fpre');
}
else
{
$data = array(
'password' => md5($this->input->post('password')),
);
$email =array(
'email' =>$this->input->post('email')
);
$this->user_model->updatePassword($data,$email);
echo "Congratulations!";
}
}
it shows no error but the password is not updated remain same at users table..i can't find the problem is, please help me to find it out ..
Controller (get_password):
public function index() {
$this->load->database();
$this->load->helper(array('form', 'url'));
$this->load->library(array('form_validation', 'session'));
$this->load->model('user_model');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Current password', 'required');
$this->form_validation->set_rules('newpassword', 'password', 'required');
$this->form_validation->set_rules('newpassconf', 'password Confirmation', 'required|matches[newpassword]');
$email = $this->input->post('email');
$success = false;
$msg = '';
if ($this->form_validation->run() !== FALSE) {
$password = md5($this->input->post('password'));
if ($this->user_model->checkPassword($email, $password)){
$newpassword = md5($this->input->post('newpassword'));
if ($this->user_model->updatePassword($email, $newpassword)){
$success = true;
}else{
$msg = 'Unable to updatePassword';
}
}else{
$msg = 'Incorrect password';
}
}
if ($success){
echo 'Congratulations!';
}else{
$this->load->view('users/fpre', array(
'email'=>$email,
'msg'=>$msg
));
}
}
Model (user_model):
public function checkPassword($email, $password) {
$users = $this->db->get_where('users', array('email'=>$email))->row();
return $users->password === $password;
}
public function updatePassword($email, $newpassword) {
$data = array('password' => $newpassword);
$this->db->where('email', $email)
->update('users', $data);
$success = $this->db->affected_rows();
if (!$success) {
error_log('Unable to updatePassword');
}
return $success;
}
View (users/fpre):
if ($msg){
echo 'Message: '.$msg;
}
echo form_open();
echo form_input('email', $email);
echo form_password('password');
echo form_password('newpassword');
echo form_password('newpassconf');
echo form_submit('', 'Enviar');
echo form_close();
Changes to compare:
Your model function shows the parameters are expected to be email and then password, but your controller is passing them through be other way around.
$this->user_model->updatePassword($data,$email);
Should be:
$this->user_model->updatePassword($email,$data);
I also believe the data needs to be passed differently. The where() function expects either where(field_name, value) or where(array(field_name => value)). Looking at your code, you seem to be mixing both of those.
Using set() should help with this too, so instead of
$data1=array('password'=>$data);
$this->db->where('email','$email');
$this->db->update('users','password');
Use:
$this->db->set($data);
$this->db->where($email);
$this->db->update('users');
Note: code untested.
I believe this line $this->db->update('users','password'); should be $this->db->update('users', $data);.
Right now you are not passing the password to the update function. You are passing the string "password".
I am working in Codeigniter registration and login forms. I have created them all is working. now what i need to auto logged in user when it successful registered on the site.
Add user code:
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);
}
Registration and validation:
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();
}
}
In else part,
else {
$id = $this->user_model->add_user();
// set session here like how you will set on login
$data["user_id"] = $id;
...// other required data for session
$this->session->set_userdata($data);
$this->thank();
}
In model return last insert id after inserting the data
$this->db->insert('user', $data);
return $this->db->insert_id();// <--- this line
I have used codeigniter code in the below when I create a user if it is already exist it should display a message duplicate value but it displays 1062 error. Pls help to solve the issue.
Controller
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');
}
}
}
Model
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);
if ($this->db->_error_number() == 1062)
{
$this->session->set_flashdata('duplicate_email', 'Duplicate value');
redirect('login');
}
return $insert;
}
It seems that your error is because your database is not allowing to add multiple entries with same username/email, for this you need to add codeigniter is_unique validation on that particular field. Below is example for codeigniter unique validation : $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[membership.username]');
Try to use $this->db->query instead of insert and create a SQL statement using INSERT IGNORE and you can handle the duplicates silently.
Why just not do a previous query to check if the email exists aleady ?
Anyway, you can do something like that:
$query_string = $this->db->insert_string('users', $data);
$this->db->query($query_string.' ON DUPLICATE KEY UPDATE id=id');
// or
$this->db->query(str_replace("INSERT INTO", "INSERT IGNORE INTO", $query_string));
if( $this->db->affected_rows() == 0 ) {
//duplicate email
}
I have form validation like this :
function insert() {
$this->form_validation->set_message('required', '*column must be filled');
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() == FALSE) {
$this->layout->addJs('ckeditor/ckeditor.js');
$this->layout->addJs('js/admin/b267648789c30a07b0efa5bce7bdd9fe.js');
$this->layout->view('admin/admin_view/insertAdmin_view');
} else {
$data = array(
'title' => $this->input->post('judul'),
'content' => $this->input->post('isi'),
);
$this->db->insert('newses', $data);
... other proses ...
}
}
although I filled the data in form add data (I use ckeditor for adding data), condition always false so it's not adding data that I want. but it's got error 'if I add some html data, but if I add normal data text it's work well'. once again when I running my program in localhost xampp it's all work but when I use hosting that happen.
any suggest ?
thank you for all advice.
Print the errors of validation
function insert() {
$this->form_validation->set_message('required', '*column must be filled');
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
//echo validation_errors();
echo $this->input->post('judul')."<br>";
echo $this->input->post('isi');
if ($this->form_validation->run() == FALSE) {
$this->layout->addJs('ckeditor/ckeditor.js');
$this->layout->addJs('js/admin/b267648789c30a07b0efa5bce7bdd9fe.js');
$this->layout->view('admin/admin_view/insertAdmin_view');
} else {
$data = array(
'title' => $this->input->post('judul'),
'content' => $this->input->post('isi'),
);
$this->db->insert('newses', $data);
... other proses ...
}
}