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>
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>
Additional Tips
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!