试图获取非对象的属性“电子邮件”

时间:2019-05-27 06:23:33

标签: php codeigniter validation

当我输入错误的电子邮件时,我从Codeigniter中编写的代码中遇到错误。

  

遇到PHP错误严重性:注意

     

消息:试图获取非对象的属性“电子邮件”

     

文件名:controllers / forms.php

     

行号:26

     

回溯:

     

文件:   E:\ Software \ XAMPP \ xampp \ htdocs \ ciauth \ application \ controllers \ forms.php   行:26函数:_error_handler

     

文件:E:\ Software \ XAMPP \ xampp \ htdocs \ ciauth \ index.php行:315   功能:require_once

下面是控制器

<?php 
class Forms extends CI_Controller 
{
    public function __construct()
    {

        parent::__construct();
        $this->load->database();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->model('user_model', 'auth');
    }

   public function forgot_pass()
    {
        if($this->input->post('forgot_pass'))
        {
            $email=$this->input->post('email');
            $this->load->model('user_model', 'auth');
            $this->load->library('form_validation');
            $this->form_validation->set_rules('email', 'E-mail', 'required');

            if ($this->form_validation->run() == TRUE) {
            $que=$this->db->query("select password,email from users where email='$email'");         
            $row=$que->row();
            $user_email=$row->email;
            if((!strcmp($email, $user_email)))
            {
                $pass=$row->password;
                $to = $user_email;
                $subject = "Password";
                $txt = "Your password is $pass .";
                $headers = "From: user@testdomain.com" . "\r\n" . "CC: hamza_zon@outlook.com ";
                mail($to,$subject,$txt,$headers);
                $this->load->view('user/header');
                $this->load->view('user/confirm');
                $this->load->view('user/footer');
                }
            else{
            $data['error']="Invalid Email ID !";
            }
        }
            else{
                $data['error']="Email ID is required !";
            }
    }
            $this->load->view('user/header');   
            $this->load->view('user/forgot_pass',@$data);
            $this->load->view('user/footer');   

   }
}
?>

5 个答案:

答案 0 :(得分:2)

您应在$user_email=$row->email;之前检查您的条件记录是否存在,如果行不存在,则会出现该错误

所以您应该检查以下内容

$row=$que->row();
if($row) {

   $user_email=$row->email;
}

答案 1 :(得分:1)

尝试这样

$que = $this->db->query("select password,email from users where email=".$email);

if(isset($row) && $row != ''){
  $txt = "Your password is" . $pass;
  //Coding...
}

答案 2 :(得分:1)

应用条件为,因为在您的情况下,它可能无法从表格中获得该电子邮件的结果,

if (isset($row))
{
  

注意: row方法返回单个结果行。如果查询有多个行,则仅返回第一行。结果是   作为对象返回。这是一个用法示例:

$que=$this->db->query("select password,email from users where email like '$email'");         

$row = $que->row();

if (isset($row))
{
    $user_email = $row->email;
    $user_password = $row->password;
}

此处简明doc

答案 3 :(得分:1)

您需要这样处理

$query = $this->db->query("select password,email from users where email=".$email);
if ($query->num_rows() > 0) {
    $row = $query->row();
    $user_email = $row->email;
} else {
    $user_email = '';
}
//then your next if condition in which you are comparing two strings

答案 4 :(得分:1)

$ row = $ que-> row();

$ row可能以NULL或伪造的值返回。

var_dump($ row)进行检查。

if(!$row)
{
    die("user not found");
}

if(empty($row))
{
    die("user not found");
}