2017-08-26 200 views
0

我正在使用笨3.PHP錯誤:陣列對象轉換成關聯數組

每當編輯記錄和數據是無效一個 CRUD應用程序,而不是僅僅示出了驗證錯誤消息,該瀏覽器輸出消息:Trying to get property of non-object。在控制器

我的更新功能如下:

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]); 
} 
} 

如果在更新表單中的數據是有效的,沒有錯誤。的形式是這樣的:

<?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(); ?> 

相信$customer變成關聯數組,這是該錯誤消息的原因:Trying to get property of non-object。但是,這種轉換的原因是什麼?或者是問題的原因完全不同?

回答

0

But what is causing this conversion?

沒什麼,你傳遞一個數組,你得到一個數組,你試圖訪問像一個對象。

在您的示例中,$data是一個數組,但在您的視圖中,您正在像訪問對象一樣訪問它。

如果你真的想進入它像一個對象,投數組對象:

$this->load->view('update', ['customer' => (object)$data]); 
+0

甜!任何可能優化上面的代碼? –

+0

什麼是優化?我沒有看到有太多的事情要做,因爲這只是一個簡單的形式,只是當你在視圖中回顯變量時要小心,確保你編碼它們以避免任何XSS問題。 – Twisted1919

+0

開箱即用的CI安全嗎? –