reset password in codeigniter - mysql

i am trying to change password in database but it's not working..it printing the password correctly..but it's not save in the database..
Here is my controller:
public function resetpwd($user_id=NULL)
{
//echo $user_id;
$this->load->helper('form');
$data['msg']=array();
if($this->input->post())
{
$user_id= $this->input->post('user_id');
//echo $id;
$this->LoginModel->resetpwd($this->input->post(),$user_id);
//redirect(base_url('resetpwd/'.$id));
}
$this->load->view('Admin/resetpwd',$data);
}
Here is my Model:
function resetpwd($post='',$user_id)
{
$data=array('password'=>$post['password']);
$this->db->where('user_id', $user_id);
$this->db->update('users', $data);
return true;
print_r($data);
}
Please help me how to do this
Thank you

Please try this code.
function resetpwd($post=array(),$user_id)
You had passed post array to resetpwd function it is mendatory to get array into array variable.
Change only one line it will be working.
Thank You.

You can simply get the password in Model page using post method extract($_POST) and you can get your new password like $data=array('password'=>$password)
Your Model is-
function resetpwd($user_id)
{
extract($_POST);
$data=array('password'=>$password);
$this->db->where('user_id', $user_id);
$this->db->update('users', $data);
return true;
//print_r($data);
}

please try this code.
Controller :
class Adminuser extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('loginModel');
$this->load->helper('form');
}
public function resetpwd()
{
$data['msg']=array();
if($this->input->post())
{
$user_id= $this->input->post('user_id');
$this->loginModel->change_password($this->input->post('password'),$user_id);
}
$this->load->view('Admin/resetpwd',$data);
}
}
Your Model Function :
public function change_password($password,$user_id)
{
$data=array('password'=>$password);
$this->db->where('user_id', $user_id);
if($this->db->update('users', $data))
{
return true;
}
else{
return false;
}
}

Apply this code
function resetpwd($post, $user_id)
{
$data=array('password'=>$post['password']);
$this->db->where('user_id', $user_id);
$this->db->update('users', $data);
return true;
}

Related

Yii2 Ldap implement kosoukhov/yii2-ldap

I am newbie with Yii2 and I need to create a login system using ldap. There is not much information about it so I hope somebody can help me.
I installed kosoukhov/yii2-ldap but don't know how to implentment it from that.
I tried editing my code as below but cannot login.
LoginForm.php
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user;
/**
* {#inheritdoc}
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
public function validatePassword($attribute, $params)
{
if (!Yii::$app->ldap->validateUserCredentials('SAMAccountName', 'password')) {
$this->addError($attribute, 'Incorrect username LDAP or password.');
}
}
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
I would really appreciate some help here.
Thank you!

Can I store a repeating sql query separately and call them inside a function in model - Codeigniter

My current model is somewhat like
class select extends CI_Model{
function __construct(){
parent::__construct();}
public function get_a(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',1);
}
public function get_b(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',2);
}
public function get_c(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',3);
}
As you can see the same Join operation is repeating in all the three functions. Is there a way which I can just do this join once and use it with the where clause
And there are other questions. If that's possible is it efficient than this current method?
Repeating code is wasteful and avoidable. Here is one function that takes an argument to be used with where.
public function get_x($where){
$this->db
->select('*')
->from('table1')
->join('table2','table1.id = table2.id')
->where('x',$where);
}
if i understand you correct - you can do the following
class select extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get_a()
{
$this->initializeBaseQuery();
$this->db->where('x',1);
}
public function get_b()
{
$this->initializeBaseQuery();
$this->db->where('x',2);
}
public function get_c()
{
$this->initializeBaseQuery();
$this->db->where('x',3);
}
private function initializeBaseQuery()
{
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id');
}
}
You can try this solution for your problem :
Select modal file :
<?php
class Select extends CI_Model{
function __construct(){
parent::__construct();
}
public function get_selection($where = array()){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id', 'left');
if(!empty($where)) {
foreach ($where as $key => $value) {
$this->db->where($key,$value);
}
}
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
}
?>
Text Controller file :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function get_select(){
$where = array("x"=>"1"); // you can pass more than one key and value for condition.
$result =$this->select->get_selection($where);
echo "<pre>";
print_r($result);
exit;
}
}
}
?>
I Hope it will help you.

How to pass a variable from another function to the view in index function

I want to pass the $data variable from the findCity() function with the json return to the view in the index function.
How can I do that please ?
class HomeController extends Controller
{
public function index(Request $request)
{
$countries = Location::select('country_name')
->groupBy('country_name', 'country_name')
->get();
$cities = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
$areas = Area::get()->toTree();
return view('home', compact('areas','countries', 'cities'));
}
public function findCity(Request $request)
{
$data = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
return response()->json($data);
}
}
The Ajax code :
$(document).on('change','.regionName',function(){
//console.log("hmm its change");
var cat_id=$(this).val();
//console.log(cat_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url:'{!!URL::to('findCityName')!!}',
data:{'region_name':cat_id},
success:function(data){
//console.log('success');
//console.log(data);
//console.log(data.length);
op+='<option value="0" selected disabled>Choose City</option>';
for(var i=0;i<data.length;i++){
op+='<option value="{{ route('user.location.store', $cities) }}">'+data[i].city_name+'</option>';
}
div.find('.cityName').html(" ");
div.find('.cityName').append(op);
},
error:function(){
}
});
});
});
You can make findCity() to be an static method and call it from inside the index() method:
public function index(Request $request)
{
// The rest of your code here...
// Calling the static method
$city = self::findCity($request);
return view('home', compact('areas', 'countries', 'cities', 'city'));
}
public static function findCity(Request $request)
{
$data = Location::select('city_name', 'city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')->get();
return $data;
}
Read more about static methods in the PHP documentation.
You need to call,pass and return using a function. like this one. I think it can help you.
class HomeController extends Controller
{
public function index(Request $request)
{
$countries = Location::select('country_name')
->groupBy('country_name', 'country_name')
->get();
$cities = $this->findCity($request)
$areas = Area::get()->toTree();
return view('home', compact('areas','countries', 'cities'));
}
public function findCity($request)
{
$data = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
return response()->json($data);
}
}

Yii2 creating simple singleton

I am trying to create simple singleton in yii2 contoller. Maybe i misunderstood something about this pattern but i decide to try. So i have a CRUD. When i got one instance of that class in the db and i decide to update it, the $instance variable is empty(null). Is it because of the page reloading after the creation of the instance and that's why my static variable is set to null again? And is it even possible to make it like this or i am really, really on wrong way? Thank you in advance!
<?php
namespace backend\controllers;
use backend\models\DeliveryTime;
use yii\data\ActiveDataProvider;
use Yii;
class DeliveryTimeController extends \yii\web\Controller
{
public static $instance = null;
public function actionIndex()
{
$delivery = new ActiveDataProvider([
'query' => DeliveryTime::find()->where('id>0')
]);
return $this->render('index', ['delivery' => $delivery]);
}
public static function setInstance()
{
if(self::$instance == null){
self::$instance = new DeliveryTime();
}
return self::$instance;
}
public static function getInstance(){
return self::$instance;
}
public function actionCreate()
{
$delivery = DeliveryTimeController::setInstance();
if($delivery->load(Yii::$app->request->post()) && $delivery->save()){
self::$instance = $delivery;
return $this->redirect(['index']);
}
return $this->render('create', ['model' => $delivery]);
}
public function actionUpdate()
{
$delivery = DeliveryTimeController::getInstance();
if($delivery->load(Yii::$app->request->post()) && $delivery->save()){
return $this->render(['index']);
}
return $this->render('update', ['model' => $delivery]);
}
public function actionDelete(){
$delivery = DeliveryTimeController::getInstance();
if($delivery != null){
$delivery->delete();
return $this->redirect(['index']);
}
}
}
For design patterns i would recommend to check out
https://github.com/kamranahmedse/design-patterns-for-humans
https://github.com/edin/php-design-patterns (my github repo :)
For what you are trying it's better to just create new instance.

How to get value column where column in codeigniter 3

I have a table like:
sysopt|sysval
......................
site_url|http://domain.com/
site_title|My Website
......................
in mysql i use:
$query = $db->query("SELECT * FROM sysconfig");
while ($result = $db->fetch_array($query)) {
$settings[$result['sysopt']] = $result['sysval'];
}
But in CI:
class Sysinfo
{
var $info = array();
public function __construct()
{
$CI =& get_instance();
$settings = $CI->db->select("*")
->get("sysconfig");
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
In view i call:
<?php echo $this->Sysinfo->info->site_url; ?>
Show error.
Message: Undefined property: CI_Loader::$Sysinfo
Thankyou any solution fix.
Why just not extend the CI_Model? For me, in framework..just extend the class so you would be easier
In your model:
class Sysinfo extends CI_Model
{
var $info = array();
public function __construct()
{
//$CI =& get_instance();
$settings = $this->db->select("*")
->get("sysconfig")->result_array(); //Return result as array
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
function get_sysinfo_data()
{
return $this->info;
}
}
In your Controller
public function __construct()
{
$this->load->model('Sysinfo');
}
function index()
{
$data['arr_result'] = $this->Sysinfo->get_sysinfo_data();
$this->load->view('your_view',$data);
}
In view:
foreach($arr_result as $row_array):
var_dump($row_array); // To view your result
endforeach;
If you want Sysinfo as library..then do like this:
Your library should be name as Sysinfo.php:
class Sysinfo
{
var $info = array();
public function __construct()
{
$CI =& get_instance();
$settings = $CI->db->select("*")
->get("sysconfig")->result_array(); //Return result as array
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
function get_sysinfo_data()
{
return $this->info;
}
}
In your Controller:
public function __construct()
{
$this->load->library('Sysinfo');
}
function index()
{
$data['arr_result'] = $this->Sysinfo->get_sysinfo_data();
$this->load->view('your_view',$data);
}
Hope it helps.
The error is appearing because you haven't extended your class with CI_Model base class.
Try defining your class as:
class Sysinfo extends CI_Model{ //your code }
will get rid of this error message.