How to give “Email does not exist” using this call back function? - php

I have already tried for "Already exists", but I need help for "Does not exist" instead of "Already exists" using same function.
login.php
public function email_exist($id) {
$this->db->where('email', $id);
$query = $this->db->get('login');
if ($query->num_rows() > 0) {
$this->form_validation->set_message(__FUNCTION__, 'Already exist!');
;
return FALSE;
} else {
return TRUE;
}
}
If the email does not exist in the database, it has to show email does not exists in login page.

Check row exist
Get row having email id, if not get data then return true;
public function email_exist($id) {
$this->db->where('email', $id);
$exist = $this->db->get('login')->row_array();
if (!$exist) {
return true;
}
}
But if you want a login scenario, then you can do as
public function email_exist($id) {
$this->db->where('email', $id);
$exist = $this->db->get('login')->row_array();
if ($exist) {
// START SESSION HERE
return true;
} else {
// give email not exist message
return false;
}
}
if you login via ajax, then you can echo "email_not_exist" from controller

//controller
function email_exist($email) {
$this->load->model('model_name');
$emailArray = $this->model_name->get_data_by_email($email);
if (!empty($emailArray)) {
$this->form_validation->set_message("validate_email", "Email already exists ");
return FALSE;
} else {
return TRUE;
}
}
//model
function get_data_by_email($email) {
return $this->db->where('email', $email)->get('login)->row_array();
}

Related

Why does the validation email returns an error while registering it doesn't?

I am developing a Register/Login system with validation. Registering system is working well. For example, when I register the same email twice, the following message appears:
Email already registered!
However, when I log-in with the same e-mail and password, an error occurs. The following message appears as a validation error:
Email not registered!
Even if the email is registered in DB.
Code for e-mail validation:
<?php
public function validateEmail($par)
{
if (filter_var($par, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
$this->setErro("Invalid Email!");
return false;
}
}
public function validateIssetEmail($email, $action = null)
{
$b = $this->cadastro->getIssetEmail($email);
if ($action == null) {
if ($b > 0) {
$this->setErro("Email already registered!");
return false;
} else {
return true;
}
} else {
if ($b > 0) {
return true;
} else {
$this->setErro("Email not registered!");
return false;
}
}
}
Code for login controller:
<?php
$validate = new Classes\ClassValidate();
$validate->validateFields($_POST);
$validate->validateEmail($email);
$validate->validateIssetEmail($email,"login");
$validate->validateStrongSenha($senha);
$validate->validateSenha($email,$senha);
var_dump($validate->getErro());
Code for class login:
<?php
namespace Models;
class ClassLogin extends ClassCrud
{
# Returns user data
public function getDataUser($email)
{
$b = $this->selectDB(
"*",
"users",
"where email=?",
array(
$email
)
);
$f = $b->fetch(\PDO::FETCH_ASSOC);
$r = $b->rowCount();
return $arrData = [
"data" => $f,
"rows" => $r
];
}
}
My getIssetEmail method exists on Register code only.
# Check directly at the bank if the email is registered
public function getIssetEmail($email)
{
$b = $this->selectDB(
"*",
"users",
"where email=?",
[
$email
]
);
return $r = $b->rowCount(); // returns the amount of rows in the search
}
And ClassPassword
<?php
namespace Classes;
use Models\ClassLogin;
class ClassPassword
{
private $db;
public function __construct()
{
$this->db = new ClassLogin();
}
# Create password's hash to save in DB
public function passwordHash($senha)
{
return password_hash($senha, PASSWORD_DEFAULT);
}
# Verify if password's hash is correct
public function verifyHash($email, $senha)
{
$hashDb = $this->db->getDataUser($email);
return password_verify($senha, $hashDb["data"]["senha"]);
}
}

can't update data to database(codeigniter)

i have this code in my update function, when i try other value the table updates but when i used that value($field8) the table don't update.
public function edit_lead() {
$id = "3";
$field8 ="http://38.102.225.87/RECORDINGS/MP3/20170201-094849_7188352209-all.mp3";
$this->db->set('field8', $field8);
$this->db->where('id', $id);
$query = $this->db->update('table');
if($query) {
return true;
} else {
return false;
}
}
Simply send the id of the user as a parameter of this function..
public function edit_lead($ID) {
}

codeigniter how do i pass Value after update done and redirect to Index()

how do i pass TRUE / FALSE after update done and redirect to Index() and set
condition $viewdata['show'] to append my html sucess or something
My Controller
class Description extends CI_Controller {
public function index()
{
$viewdata['content']=$this->General_model->get_page_uri();
$viewdata['show']=; //where i want to get value when update() method
//pass value so i can show sucess / error message
$this->load->view("backend/content_view",$viewdata);
}
public function update()
{
$title=$this->input->post('txttitle');
if($title != '')
{
if(!$this->update_model->update_all($title))
{
return FALSE;
}
return TRUE;
}
redirect('Admin/Description');
}
}
My Model
public function update_all($data)
{
$this->db->set('desc',$data)
->where('desc_id','1');
if(!$this->db->update('tbl_desc'))
{
return FALSE;
}
return TRUE;
}
#Ritesh d joshi thz it work but i face problem that i can't modify when update error i test to change my field name to other to test return false;
Admin/Description/update
it show me 'A Database Error Occurred' by Codeigniter
i don't want this to show i want to keep my Admin page still same just alert nomol message error that i have set not to show many info error. how could i prevent this or this can be done by Ajax request only ?
Controller index()
if($show_ses === '0')
{
$viewdata_result = $this->General_model->rk_alert_ok('Successfully Update');
$this->session->set_flashdata('show', 'false');
}elseif($show_ses === '1'){
$viewdata_result=$this->General_model->rk_alert_no('Fail Update Request');
$this->session->set_flashdata('show', '');
}
Controller update()
if(!$this->update_model->update_all($title))
{
$this->session->set_flashdata('show', '1');
//1= false
}else{
$this->session->set_flashdata('show', '0');
//0=true
}
Use the PHP header() function.
header('Location: your_URL');
Update:
In CI, you can use redirect() function, this document will help you to understand: http://www.codeigniter.com/user_guide/helpers/url_helper.html
Please try this
class Description extends CI_Controller {
public function index()
{
$viewdata['content']=$this->General_model->get_page_uri();
$show= $this->session->flashdata('show');
if($show){
// Here is code for show and message
$viewdata['show']="message";
$this->session->set_flashdata('show', 'false');
}
$this->load->view("backend/content_view",$viewdata);
}
public function update()
{
$title=$this->input->post('txttitle');
if($title != '')
{
if(!$this->update_model->update_all($title))
{
return FALSE;
}
$this->session->set_flashdata('show', 'true');
return TRUE;
}
redirect('Admin/Description');
}
}
You can use redirection in update() as:
public function update()
{
$title = $this->input->post('txttitle');
if($title != '')
{
$status = $this->update_model->update_all($title);
if($status){
redirect(base_url().'index?show=1');
}
else{
redirect(base_url().'index?show=0');
}
}
redirect('Admin/Description');
}
than you can check the status in index() as:
public function index()
{
$viewdata['content']=$this->General_model->get_page_uri();
if(isset($this->input->post('show')) && intval($this->input->post('show')) == 0){
$viewdata['show'] = 1; // if success than show 1
}
else{
$viewdata['show'] = 0; // if error than show 0
}
$this->load->view("backend/content_view",$viewdata);
}
You can use the Header function, and to detect it you can pass the parameters too in the GET Url like below.
By default set the status as FALSE. ANd you can update the status according to your conditions either to FALSE or TRUE.
public function update()
{
$status = false;
$title=$this->input->post('txttitle');
if($title != '')
{
if(!$this->update_model->update_all($title))
{
$status = FALSE;
return FALSE;
}
$this->session->set_flashdata('show', 'true');
$status = TRUE;
return TRUE;
}
header('Location: abc.php?status='.$status);
}
Hope This will work, Do let me know in case of any confusion.

Strange query generated in Codeigniter

Below is my code where I am calling three methods from three models to retrieve counts as below.
$this->load->model('orders_model');
$order_count = $this->orders_model->count_orders(array("executive_id" => $this->id));
$this->load->model('activities_model');
$activity_count = $this->activities_model->count_activities(array("users_id" => $this->id));
$this->load->model('leads_model');
$leads_count = $this->leads_model->count_leads(array("users_id" => $this->id));
And this is the query I am getting:
SELECT COUNT(*) AS numrows FROM orders, activities, leads WHERE executive_id = '5' AND users_id = '5' AND users_id = '5'
which leads to a database error
Why is this happening?
Orders_model
class Orders_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_order($order_update_data = FALSE, $order_update_condition = FALSE) {
if (!($order_update_data === FALSE && $order_update_condition === FALSE)) {
if (is_array($order_update_data) && is_array($order_update_condition)) {
return $this->db->update('orders', $order_update_data, $order_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_orders($order_custom_condition = FALSE) {
if (!$order_custom_condition === FALSE) {
if (is_array($order_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($order_custom_condition);
$query = $this->db->get('orders');
return $query->result();
}
}
}
public function get_last_ref_id() {
$query = $this->db->query('select sprx_ref_id from orders where id in (select max(id) from orders)');
foreach ($query->result() as $row) {
return $row->sprx_ref_id;
}
}
public function fetch_orders($limit, $start, $order_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($order_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_orders($order_custom_condition) {
$this->db->where($order_custom_condition);
return $this->db->count_all_results('orders', FALSE);
}
}
Activities_model
class Activities_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_activities($activity_id = FALSE) {
if ($activity_id === FALSE) {
$query = $this->db->get('activities');
return $query->result();
}
$this->db->where('id', $activity_id);
#$this->db->order_by('id','ASC');
$query = $this->db->get('activities');
return $query->result();
}
public function add_activity($activity_data = FALSE) {
if (!$activity_data === FALSE) {
if (is_array($activity_data)) {
return $this->db->insert('activities', $activity_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_activity($activity_update_data = FALSE, $activity_update_condition = FALSE) {
if (!($activity_update_data === FALSE && $activity_update_condition)) {
if (is_array($activity_update_data) && is_array($activity_update_condition)) {
return $this->db->update('activities', $activity_update_data, $activity_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_activities($activity_custom_condition = FALSE) {
if (!$activity_custom_condition === FALSE) {
if (is_array($activity_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($activity_custom_condition);
$query = $this->db->get('activities');
return $query->result();
}
}
}
public function fetch_activities($limit, $start, $custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_activities($custom_condition) {
$this->db->where($custom_condition);
return $this->db->count_all_results('activities', FALSE);
}
}
Leads_model
class Leads_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_leads($lead_id = FALSE) {
if ($lead_id === FALSE) {
$query = $this->db->get('leads');
return $query->result();
}
$this->db->where('id', $lead_id);
$query = $this->db->get('leads');
return $query->result();
}
public function add_lead($lead_data = FALSE) {
if (!$lead_data === FALSE) {
if (is_array($lead_data)) {
return $this->db->insert('leads', $lead_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_lead($lead_update_data = FALSE, $lead_update_condition = FALSE) {
if (!($lead_update_data === FALSE && $lead_update_condition)) {
if (is_array($lead_update_data) && is_array($lead_update_condition)) {
return $this->db->update('leads', $lead_update_data, $lead_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_leads($lead_custom_condition = FALSE) {
if (!$lead_custom_condition === FALSE) {
if (is_array($lead_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($lead_custom_condition);
$query = $this->db->get('leads');
return $query->result();
} else {
return false;
}
} else {
return false;
}
}
public function fetch_leads($limit, $start, $lead_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($lead_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads', FALSE);
}
}
As far as i understand, you are surprised why the query builder uses additional parameter from the previous query.
You've to reset your Query according to docs
which means all your "count_" functions should be like
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads');
}
obviously you did set the false flag on purpose - but i'm not sure why ;)
I believe that the error message is only a symptom, not the actual cause of the issue
. The way I read your code is that the count_*() methods in each of the 3 models should return the count from their respective tables only.
However, the way you wrote your count functions results in the query builder adding the tables and conditions to the overall query, not executing them just on the single tables
$this->db->where($custom_condition); <-- this adds a new where condition using "and" operator
return $this->db->count_all_results('activities', FALSE); <-- just adds another table without resetting the others
I would add a $this->db->reset_query(); line as the 1st line in each of the 3 count_*() methods to force the query builder to start from scratch.
The problem is due to using the second argument to $this->db->count_all_results(). When you set the second argument to FALSE then $this->db will not clear any select statements from its cache. Then each successive call to count_all_results() will include the table from any prior call to the function. The solution is simple - don't use the second parameter.
Change
return $this->db->count_all_results('activities', FALSE);
to
return $this->db->count_all_results('activities');
Not related to your problem but something that will improve your code is changing this
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
to
public function get_orders($order_id = NULL) {
if (!empty($order_id))
{
$this->db->where('id', $order_id);
}
$query = $this->db->get('orders');
return $query->result();
}
Changing the argument default to NULL and using !empty($order_id) helps because it protects against and empty string or empty array being given as the argument. (Ready about empty() here.)
This new logic also keeps the code DRY - your not repeating the two line of code to get and return results.
Many of your other model functions could be cleaner too. For instance
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
would be cleaner written like this
public function add_order($order_data = NULL) {
if (!empty($order_data) && is_array($order_data))
{
return $this->db->insert('orders', $order_data);
}
return false;
}
Sorry for nitpicking your code - I couldn't help myself.

having trouble in codeigniter passing result or method from model to controller?

I am trying to create a basic login, no error checking or anything yet, in an effort to get used to codeigniter. Below is my controller class method that I am attempting to pass the result from my model method back in to verify username and password.
public function login()
{
if (isset($_POST['email'])) {
$this->cdata['email'] = $_POST['email'] ;
} else {
$this->cdata['email'] = "";
}
if (isset($_POST['password'])) {
$this->cdata['password'] = $_POST['password'];
} else {
$this->cdata['password'] = "";
}
$this->load->model("dbaccess");
$this->loggedin = $this->dbaccess->check_input($this->cdata['email'], $this->cdata['password']);
if($this->loggedin == TRUE) {
$this->load->view('carerview', $this->cdata);
} else {
$this->cdata['warning'] = "Check failed ! Please try again";
$this->load->view('mainview', $this->cdata);
}
}
The post from my view seems to be working fine. The post is sent back to the main login/index, to the method login (shown above) below shows my model class which is called in my login method in the controller it only has one method so far. check_input()
class Dbaccess extends CI_Model
{
function __construct()
{
parent::__construct();
}
function check_input($email, $password)
{
$this->db->select('email');
$this->db->from('tablename');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return TRUE;
} else {
return FALSE;
}
}
}
When I hit submit on my index page I keep getting the warning no matter what and I can't figure out where the issue is.
Try if it works:
function login()
{
if( $this->input->post( null ) ){ #check if the post array is not blank
$this->load->model("dbaccess");
$this->loggedin = $this->dbaccess->check_input($this->input->post('email'),$this->input->post('password'));
}else{
$this->loggedin = false;
}
if($this->loggedin == TRUE)
{$this->load->view('carerview',$this->cdata);}
else
{$this->cdata['warning']="Check failed ! Please try again";
$this->load->view('mainview',$this->cdata);
}
}
Is your table actually called tablename? Just to try and debug it, try this inside your check_input function and post the results (changing the username/pass if needed):
function check_input($email,$password)
{
var_dump($email);
var_dump($password);
$this->db->select('email');
$this->db->from('tablename');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
echo $this->db->last_query();
if (!$query) {
// if query returns null
$msg = $this->db->_error_message();
exit("Error: ".$msg);
}
if($query->num_rows() > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}

Categories