尝试在codeigniter中获取非对象的属性的消息

时间:2014-12-06 12:04:33

标签: php codeigniter

查看: 任何人帮我解决这个问题,我得到以下错误遇到PHP错误

Severity: Notice

Message: Trying to get property of non-object

Filename: users/dataupdate.php

Line Number: 5

这是我的观看页面

dataupdate.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;
   }
}

?>

3 个答案:

答案 0 :(得分:0)

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

<?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>

答案 1 :(得分:0)

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

以此为例。一个对象Car $ car存在于您的代码中,Car对象具有公共属性$ speed。通常你可以通过$ car-&gt; speed来访问$ car对象的速度。如果以某种方式将$ car设置为null或其他值(不是Car对象),则在尝试访问Car的$ 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对象的损坏位置和解决它。

答案 2 :(得分:0)

只需使用像@$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">