doudou8081 2017-08-26 12:30
浏览 39
已采纳

PHP错误:数组对象转换为关联数组

I am making a CRUD application using Codeigniter 3.

Whenever I edit a record and the data is invalid, instead of just showing the validation error messages, the browser outputs the message: Trying to get property of non-object.

My update function inside the controller looks like this:

public function update($customer_id) {
    // data validation
    $this->form_validation->set_rules('first_name', 'First name', 'required');
    $this->form_validation->set_rules('last_name', 'Last name', 'required');
    $this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
    $this->form_validation->set_rules('phone', 'Phone number', 'required');
    $this->form_validation->set_rules('country', 'Country', 'required');
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

    if ($this->form_validation->run()) {
        $data = [
        // insert into these database table fields
        'first_name' => $this->input->post('first_name'),
        'last_name' => $this->input->post('last_name'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
        'country' => $this->input->post('country'),
        'city' => $this->input->post('city'),
        'address' => $this->input->post('address')
        ];
        $this->load->model('Customer');
        if ($this->Customer->updateCustomer($customer_id, $data)) {
            $this->session->set_flashdata('update-response','Customer successfully updated');
        } else {
            $this->session->set_flashdata('update-response','Failed to update customer');
        }
        redirect('home');
    } else {
    $data = [       
        'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'country' => $this->input->post('country'),
            'city' => $this->input->post('city'),
            'address' => $this->input->post('address'),
        'id' => $customer_id
    ];
    $this->load->view('update', ['customer' => $data]);
 }
}

If the data in the update form is valid there are no errors. the form looks like this:

<?php echo form_open("home/update/{$customer->id}"); ?>

            <div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
                <?php echo form_input('first_name', set_value('first_name', $customer->first_name),[
                    'id' => 'first_name',
                    'class' => 'form-control'
                    ]);
                if(form_error('first_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
                echo form_error('first_name'); ?>                                     
            </div>

            <div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
                <?php echo form_input('last_name', set_value('last_name', $customer->last_name), [
                    'id' => 'last_name',
                    'class' => 'form-control'
                    ]);
                if(form_error('last_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
                echo form_error('last_name'); ?> 
            </div>

            <div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
                <?php echo form_input('email', set_value('email', $customer->email), [
                    'id' => 'email',
                    'class' => 'form-control'
                    ]);
                if(form_error('email')) echo '<span class="glyphicon glyphicon-remove"></span>';
                echo form_error('email'); ?> 
            </div>

            <div class="form-group <?php if(form_error('phone')) echo 'has-error';?>">
                <?php echo form_input('phone', set_value('phone', $customer->phone), [
                    'id' => 'phone',
                    'class' => 'form-control'
                    ]);
                if(form_error('phone')) echo '<span class="glyphicon glyphicon-remove"></span>';
                echo form_error('phone'); ?>
            </div>

            <div class="form-group <?php if(form_error('country')) echo 'has-error';?>">
                <?php echo form_input('country', set_value('country', $customer->country), [
                    'id' => 'country',
                    'class' => 'form-control'
                    ]);
                if(form_error('country')) echo '<span class="glyphicon glyphicon-remove"></span>';
                echo form_error('country'); ?>
            </div>

            <div class="form-group">
                <?php echo form_input('city', set_value('city', $customer->city), [
                    'id' => 'city',
                    'class' => 'form-control'
                    ]);
                ?>
            </div>

            <div class="form-group">
                <?php echo form_input('address', set_value('city', $customer->address), [
                    'id' => 'address',
                    'class' => 'form-control'
                    ]);
                ?>
            </div>

            <div class="form-group">
                <?php echo form_submit('submit', 'Save', 'class = "btn btn-success btn-block"'); ?>
            </div>

<?php echo form_close(); ?>

I believe $customer is turned into an associative array and this is the reason for the error message: Trying to get property of non-object. But what is causing this conversion? Or is the cause of the problem completely different?

  • 写回答

1条回答 默认 最新

  • dongzhang1864 2017-08-26 12:35
    关注

    But what is causing this conversion?

    Nothing, you pass an array and you get an array which you try to access like an object.

    In your example, $data is an array, but in your view you are accessing it like an object.

    If you really want to access it like an object, cast the array to object:

    $this->load->view('update', ['customer' => (object)$data]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。