Home  >  Article  >  Database  >  How to Fix \"Trying to get property of non-object\" Error in CodeIgniter Edit Form?

How to Fix \"Trying to get property of non-object\" Error in CodeIgniter Edit Form?

Susan Sarandon
Susan SarandonOriginal
2024-10-24 19:24:29246browse

How to Fix

Error: "Trying to Get Property of Non-Object" in CodeIgniter Edit Form

When attempting to edit records in CodeIgniter, you may encounter the "Trying to get property of non-object" error when populating your edit form. This error indicates that the $product variable used to retrieve data is not an object.

To resolve this issue, use array notation to access the array elements rather than object notation. Instead of $product->prodname, use $product['prodname'].

In your edit_product_view.php file, update the following:

<code class="php"><td><?php echo form_label('Name:'); ?></td>
<td><?php echo form_input('prodname', set_value('prodname', $product['prodname'])); ?></td>

<td><?php echo form_label('Product Type:'); ?></td>
<td><?php echo form_dropdown('ptname_fk', $product_types, set_value('ptname_fk', $product['ptname_fk'])); ?></td></code>

Array notation accesses the elements directly from the array, resolving the "Trying to get property of non-object" error. Your edit form will now correctly populate with the data retrieved from the database.

The above is the detailed content of How to Fix \"Trying to get property of non-object\" Error in CodeIgniter Edit Form?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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