where() showing "IS NULL" instead of variable value in codeigniter - php

$email='abc#abc.com';
$pass= 123;
$this->db->select('*')->from('user')->where('email',$email)->where('password',$pass)-> get()->result();
this gives result :-
'select * from user where email IS NULL and password IS NULL';
But it works with $this->db->query();
plz help me to find why its not taking like :-
'select * from user where email="abc#abc.com" and password="123" ';

Use this method. Hope it will help you
$email = "abc#example.com";
$password = "abc123";
public function getdata()
{
// first load your model if not added in your config/autoload file
return $this->db->get_where('table_name', array('email' => $email, 'password' => $password))->row_array();
}

Related

How do I set a variable `true` when updating the database has been successful?

On a blog I'm coding the admin can give an 'author'-permission to users.
When the update of the db table has been successful and their permission has been set to 'author' the admin will be headed back to the list of all current authors.
I want a message("Author has been added." for e.g) to appear on this site when it has been successful.
Of course the possibility of the db-update not working is minimal I think, but I want this case to be considered.
To do this I wanted to set a $newAuthor true when the database has been updated, but it didn't worked trying it with an if.
Here are the functions in the AdminController and the UserRepository with the db query:
//AdminController
public function permissionAuthor()
{
$id = $_GET['id'];
$permission = "author";
$newAuthor = false;
if($this->userRepository->changePermission($id, $permission)) {
$newAuthor = true;
}
header("Location: authors");
}
//UserRepository
public function changePermission($id, $permission)
{
$table = $this->getTableName();
$stmt = $this->pdo->prepare(
"UPDATE `{$table}` SET `permission` = :permission WHERE `id` = :id");
$changedPermission = $stmt->execute([
'id' => $id,
'permission' => $permission
]);
return $changedPermission;
}
// authors.php / the view
<?php if(isset($newAuthor) && $newAuthor == true):?>
<p class="error">Author has been added.</p>
<?php endif;?>
How can I achieve that $newAuthor will only be set to true when the function that updates the database has been successful and the message to be displayed in the view?
EDIT
I tried it with returning $changedPermission in the UserRepository. It might be wrong because it hasn't changed anything.
You can either check the permission before changing it and see if there's a difference, or just check if the UPDATE request worked successfully.
Since the prototype: public PDOStatement::execute ([ array $input_parameters ] ) : bool
You can check if the request has been successful by verifying the return value of the execute function like that:
$result = $stmt->execute([
'id' => $id,
'permission' => $permission
]);
if ($result == FALSE)
echo 'ERROR';
else
echo 'ok';
Also directly put $newAuthor = $this->userRepository->changePermission($id, $permission); in permissionAuthor function.
But one more thing, I don't see where you are calling your permissionAuthor function in your code ? Are you sure it's executed ?

mysqli_insert_id() expects exactly 1 parameter, 0 given in CodeIgniter

I have a problem, Please Help me. I'm using Code igniter
This is my code.
my Models (M_register.php)
class M_register extends CI_Model {
function __construct()
{
parent::__construct();
}
function add_account($data)
{
$this->load->database();
$this->db->insert('login',$data);
**return mysqli_insert_id();**
}
function changeActiveState($key)
{
$this->load->database();
$data = array(
'active' => 1
);
$this->db->where('md5(id)', $key);
$this->db->update('login', $data);
return true;
}
}
And this is partly code in Controller
$data = array(
'username' => $username,
'password' => $password,
'nama' => $nama,
'email' => $email,
'active' => 0
);
$this->load->model('m_register');
**$id = $this->m_register->add_account($data);**
$encrypted_id = md5($id);
enter code here
And this My error:
The error message is clear :
mysqli_insert_id() expects exactly 1 parameter, 0 given
You must give it the connection in parameter. For example :
$con = mysqli_connect("localhost","my_user","my_password","my_db");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");
// Here the call to "mysqli_insert_id" takes the output of "mysqli_connect" as parameter
echo "New record has id: " . mysqli_insert_id($con);
mysqli_close($con);
Source: https://www.w3schools.com/php/func_mysqli_insert_id.asp
I don't know in your code where you're doing the connection, but check that your call to $this->load->database() doesn't return the connection resource.
Pass the database connection variable as parameter
$query = $this->db->insert('login',$data);
return mysqli_insert_id($dbconnection);
You are using codeigniter, so it would be better if you use codeigniter functions only to perform database operation.
Definitely you will get result using mysqli_insert_id(db_connection_object)
But it would be better if you use inbuilt function of codeigniter
$this->db->insert_id();

how to send data from controller to model with CodeIgniter

i'm newbie with CI and i'm using CI v.2.2.1, i want send data from my controller to model but i have some trouble here. The error said my model is undefined property. here's my code :
Controller :
users.php
public function postUser(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$address = $this->input->post('address');
$level = $this->input->post('level');
$status = $this->input->post('status');
$this->load->model('model_crud');
$query = $this->model_crud->insert('Users',$_POST);
echo "$query";
}
Model :
model_crud.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_crud extends CI_Model {
public function __construct(){
parent::__construct();
}
public function insert($table,$data){
$query = $this->db->insert($table,$data);
return $query;
}
}
is there any configuration to use model or my code is wrong?
can anyone help me ? Thx
1st thing, you're not providing enough information about the error.
Is the model loaded anywhere/anyhow? From the controller you load your model this way:
$this->load->model('model_crud');
Or you can preload it, by modifying your autolad.php config file
in postUser() - you're getting your post data, but don't really use it. Instead, you're passing the whole global $_POST, which may be dirty and unsafe. I'd recommend using CodeIgniter's XSS filtering when forming a data array from POST:
$data = array (
'username' => $this->input->post('username', TRUE); //TRUE identifies you're passing your data through XSS filter,
//all other elements
);
finally:
$query = $this->model_crud->insert('Users',$data);
You can do it by sending an array to your model with table columns name as key :
Controller :
$data = array(
'username' => $this->input->post('username') ,
'password ' => $this->input->post('password') ,
...
);
// 'TABLE COLUMN NAME' => "VALUE"
$query = $this->model_crud->insert('Users',$data);
echo "$query";

Call to a member function - Codeigniter

I try to do login page with session but face a problem.
First i created a model called giris. the model giris has a function girisKontrol
function girisKontrol($username, $password) {
$sha1_password = sha1($password);
$query = "SELECT id FROM pasaj_register WHERE username = '".$username."' and password = '".$sha1_password."'";
$result = $this->db->query($query, array($username, $sha1_password));
if ($result->num_rows() == 1)
return $result->row(0)->id;
else
return false;
}
and in a controller called giris
wrote below code,
public function main_page() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$userID = $this->giris->girisKontrol($username,$password);
if (!$userID) {
$this->session->set_flashdata('login error', TRUE);
redirect('giris/giris');
} else {
$this->session->set_userdata(array(
'logged_in' => TRUE,
'userID' => $userID));
redirect('welcome_message');
}
}
however when form is processed. i take below error,
What's the reason ?
You probably haven't loaded the giris model.
Put something like this inside the constructor of the controller or at the top of the function call of the controller to load the model.
$this->load->model('giris');
You probably forgot to load your model:
Invoke
$this->load->model('giris');
prior to
$userID = $this->giris->girisKontrol($username,$password);
the problem is that your class and your model has the same name...change your class to something like girisVO, report back

Codeigniter help with user registration script

:) This question is for anyone familiar with the Tankauth plugin.
I edited tankauth so that user profiles are populated without email validation.
In the registration form I added and validated two more fields named “first_name” and “last_name” which I want to add to the user_profiles table when the form is submitted.
Below is the tankauth function that adds the user id to the user_profiles table if user is activated. I commented out if ($activated). The problem is that I don’t know how to insert my new fields first_name and last_name to the db. I keep getting errors for anything I try.
function create_user($data, $activated = TRUE)
{
$data['created'] = date('Y-m-d H:i:s');
$data['activated'] = $activated ? 1 : 0;
if ($this->db->insert($this->table_name, $data)) {
$user_id = $this->db->insert_id();
//if ($activated)
$this->create_profile($user_id);
return array(
'user_id' => $user_id,
'first_name' => $first_name, //added by me
'first_name' => $first_name, //added by me
);
}
return NULL;
}
The above is connected with
private function create_profile($user_id)
{
$this->db->set('user_id', $user_id);
$this->db->set('first_name', $first_name); //added by me
return $this->db->insert($this->profile_table_name);
}
Can someone please share some guidance?
Try removing the private in private function create_profile
Also you might want to change your create_profile function to be:
function create_profile( $user_id ) {
$data = array(
'user_id' => $user_id,
'first_name' => $first_name // Hopefully this is being set correctly.
);
return $this->db->insert( $this->profile_table_name, $data );
}
If none of those work. Try using echo $this->db->last_query();
Hope that helps.

Categories