尝试在代码点火器中获取非对象的属性的消息


message trying to get property of non-object in codeigniter

查看:任何人都可以帮助我解决此问题,我遇到以下错误PHP错误

Severity: Notice
Message: Trying to get property of non-object
Filename: users/dataupdate.php
Line Number: 5

这是我的视图页面

数据更新.php:

<form method="post" action="<?php echo base_url();?>user_controller/dataupdate/<?php echo $id;?>">
    <table width="280" border="1" align="center">
      <tr>
        <td>Name</td>
        <td><input type="text" name="name" value="<?php $result->name;?>"></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><input type="test" name="cnum" value="<?php $result->email;?>></td>
      </tr>
      <tr>
        <td>mobile</td>
        <td><input type="password" name="pass" value="<?php $result->mobile;?>></td>
      </tr>
      <tr>
        <td colspan="2" align="center"><input type="submit" name="sub" value="Submit"></td>
      </tr>
    </table>
</form>

这是我的控制器

user_controller.php:

<?php
    class user_controller extends CI_controller
    {
        function __construct()
        {
            parent:: __construct();
            //$this->load->helper('form');
            //$this->load->helper('url');
            $this->load->model('user_model');
        }
        function add()
        {
            $data['title']="add";
            $this->form_validation->set_rules("name","Name","required");//text fildname,userdefine name
            $this->form_validation->set_rules("email","email","required");
            $this->form_validation->set_rules("password","password","required");
            $this->form_validation->set_rules("mobile","mobile","required");

            if($this->form_validation->run())
            {
                $this->user_model->insert($_POST);
                redirect("user_controller/display");
            }
            $this->load->view('users/add',$data);
        }
        function display()
        {
            $data['result']=$this->user_model->datadisplay();
            $this->load->view('users/datadisplay',$data);
        }
        function deletedata($id)
        {
            $data['result']=$this->user_model->deletequery($id);
            redirect("user_controller/display");
        }
        function updatedata($id)
        {
             $data['result']=$this->user_model->datadisplay(array("id"=>$id));
             if($this->form_validation->run())
             {
                  $_POST['id']=$id;
                  $id=$this->user_model->update($_POST);
             }
             $data['id']=$id;
             $this->load->view('users/dataupdate',$data);
        }
    }
?>

这是我的模型

user_model.php:

<?php
class user_model extends CI_model
{
    function insert($options=array()){
        if(isset($options['name']))//userdefine name
            $this->db->set('name',$options['name']);//db colomn name,text field name
        if(isset($options['email']))
            $this->db->set('email',$options['email']);
        if(isset($options['password']))
            $this->db->set('password',md5($options['password']));
        if(isset($options['mobile']))
            $this->db->set('mobile',$options['mobile']);
        $this->db->insert('user');
        return $this->db->insert_id();
    }
    function datadisplay()
    {
        $this->db->select('*');
        $this->db->from('user');
        if(isset($options['id']))
            $this->db->where('id',$options['id']);
        $query=$this->db->get();
        return $query->result();
    }
    function deletequery($id)
    {
        $this->db->delete('user',array('id'=>$id));
    }
    function update($options=array())
    {
        if(isset($options['name']))//userdefine name
            $this->db->set('name',$options['name']);//db colomn name,text field name
        if(isset($options['email']))
            $this->db->set('email',$options['email']);
        if(isset($options['mobile']))
            $this->db->set('mobile',$options['mobile']);
        if(isset($options['id']))
            $this->db->where('id',$options['id']);
        $query=$this->db->update('user');
        return $query;
   }
}
?>

你应该将值分配给另一个变量而不是使用它。

<?php    
$tempname = $result[0]->name; 
$email = $result[0]->email;
$mob = $result[0]->mobile;
?>

<form method="post" action="<?php echo base_url();?>user_controller/dataupdate/<?php echo $id;?>">
<table width="280" border="1" align="center">
  <tr>
    <td>Name</td>
    <td><input type="text" name="name" value="<?php echo $tempname;?>"></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="test" name="cnum" value="<?php echo $email;?>></td>
  </tr>
  <tr>
    <td>mobile</td>
    <td><input type="password" name="pass" value="<?php echo $mob;?>></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" name="sub" value="Submit"></td>
  </tr>
</table>
</form>

当您在 PHP 中收到消息"尝试获取非对象的属性"时,这意味着您正在尝试访问对象的属性,而该对象不存在。

以这个为例。 代码中存在对象 Car $car,并且 Car 对象具有公共属性$speed。通常,您可以通过$car->速度访问$car对象的速度。如果以某种方式将 $car 设置为 null 或某个其他值(如果它不是 Car 对象(,则在尝试访问 Car 的 $speed 属性时,您将收到错误。

在尝试访问 speed 属性之前,您可以执行以下操作:

if (is_a($car, 'Car')) {
    //do whatever you want when $car is a Car object
} else {
    //do whatever you want when $car is not a Car object
}

你也可以使用这个:

if ($car instanceof Car) {
    //do whatever you want when $car is a Car object
} else {
    //do whatever you want when $car is not a Car object
}

最后,您可以执行此操作,这将在$car不是 Car 对象时禁止异常:

if (@$car->speed)
    //do whatever you want when $car is a Car object
} else {
    //do whatever you want when $car is not a Car object
}

如果$car始终应该是 Car 对象,并且您收到错误,最好的办法是避免所有其他逻辑并确定您的 $car 对象在哪里被损坏并修复它。

简单地说,你可以使用对象或变量,如@$variable

 <input name="ftp_name"  value="<?php echo $ftpDetails->ftp_name;?>" type="text">

更改为:

<input name="ftp_name"  value="<?php echo @$ftpDetails->ftp_name;?>" type="text">