【问题标题】:Trying to get property 'email' of non-object试图获取非对象的属性“电子邮件”
【发布时间】:2019-05-27 06:23:33
【问题描述】:

当我输入错误的电子邮件时,我用 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');   

   }
}
?>

【问题讨论】:

  • 那么如何处理邮件不在表格中的情况呢?您的代码假定电子邮件存在。
  • 我该怎么做?
  • 看起来好像您在数据库中存储纯文本密码 - 这不是一个好主意!尝试寻找更好的方法来帮助忘记密码的用户,而不是这样。

标签: php codeigniter validation


【解决方案1】:

您应该在$user_email=$row-&gt;email; 之前检查是否存在根据您的条件记录,如果行不存在,那么您将收到该错误

所以你应该检查如下

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

   $user_email=$row->email;
}

【讨论】:

  • 遇到 PHP 错误 严重性:通知消息:未定义变量:user_email 文件名:controllers/forms.php 行号:29 回溯:文件:E:\Software\XAMPP\xampp\htdocs\ciauth\ application\controllers\forms.php 行:29 函数:_error_handler 文件:E:\Software\XAMPP\xampp\htdocs\ciauth\index.php 行:315 函数:require_once
  • 你需要把找到记录后要执行的所有代码放在if($row) {...
【解决方案2】:

这样试试

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

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

【讨论】:

    【解决方案3】:

    你需要这样处理,

    $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
    

    【讨论】:

    • 发生数据库错误 错误编号:1054 'where 子句'中的未知列 'fffff' 选择密码,来自用户的电子邮件,其中 email=fffff 文件名:E:/Software/XAMPP/xampp/htdocs/ciauth /system/database/DB_driver.php 行号:691
    【解决方案4】:

    $row=$que->row();

    $row 可能被返回为 NULL 或假值。

    var_dump($row) 来检查。

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

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

    【讨论】:

    • 感谢您的回答,它有效,而不是使用“死”,我想将用户重定向到同一页面并显示错误
    • 使用php函数header("location:page");
    【解决方案5】:

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

    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

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      相关资源
      最近更新 更多