Home > Database > Mysql Tutorial > Why Am I Getting the \'Trying to Get Property of Non-Object\' Error in CodeIgniter?

Why Am I Getting the \'Trying to Get Property of Non-Object\' Error in CodeIgniter?

Susan Sarandon
Release: 2024-10-24 18:40:47
Original
715 people have browsed it

Why Am I Getting the

Understanding the "Trying to Get Property of Non-Object" Error in CodeIgniter

When attempting to update a database record using CodeIgniter, you may encounter the "Trying to get property of non-object" error. This issue arises when attempting to access properties of an object that is not an instance of a class.

In the context of your edit_product_view, you are trying to populate a form using the $product object, which is retrieved based on the product ID selected. However, you are accessing its properties using object notation ($product->prodname).

Resolving the Issue: Object vs. Array Notation

CodeIgniter stores retrieved data as arrays, not objects. Therefore, you should use array notation to access individual elements of the $product array, which contains the values for your form fields.

Replace the following lines:

<code class="php"><?php echo form_input('prodname', set_value('prodname', $product->prodname)); ?>
<?php echo form_dropdown('ptname_fk', $product_types, set_value('ptname_fk', $product->ptname_fk)); ?></code>
Copy after login

with:

<code class="php"><?php echo form_input('prodname', set_value('prodname', $product['prodname'])); ?>
<?php echo form_dropdown('ptname_fk', $product_types, set_value('ptname_fk', $product['ptname_fk'])); ?></code>
Copy after login

Additional Tips

  • Ensure that your $product array contains the correct key-value pairs.
  • If you still encounter the error, check that you have properly passed the $product variable to your view.
  • Consider using print_r to inspect the contents of your $product array to verify that it contains the expected data.

The above is the detailed content of Why Am I Getting the \'Trying to Get Property of Non-Object\' Error in CodeIgniter?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template