Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help figuring why I get the following error: Fatal error: Call to a member function update() on a non-object in
    primarykey
    data
    text
    <p>I am still learning the pdo and php in general, I am creating a CRUD from a tutorial and converting the mysql into PDO but am getting the following two errors and am not quite sure why. The errors occur when I am testing out the update part of the code using a specific "find_by_id" method in user.php. Without the method updating into mysql works fine.</p> <p>When I run find_by_id it in turn runs find_by_sql which also runs instantiate and has_attributes. I var_dumped $found from find_by_sql and get the following:</p> <p>stdClass Object ( [id] => 6 [username] => bingo [password] => 123 [first_name] => bingo [last_name] => bongo )</p> <p><strong>Warning: Attempt to assign property of non-object in C:\xampp\htdocs\projects\photo_gallery\public\admin\test.php on line 20</strong></p> <p><strong>Warning: Attempt to assign property of non-object in C:\xampp\htdocs\projects\photo_gallery\public\admin\test.php on line 21</strong></p> <p><strong>Warning: Attempt to assign property of non-object in C:\xampp\htdocs\projects\photo_gallery\public\admin\test.php on line 22</strong></p> <p><strong>Warning: Attempt to assign property of non-object in C:\xampp\htdocs\projects\photo_gallery\public\admin\test.php on line 23</strong></p> <p><strong>Warning: Attempt to assign property of non-object in C:\xampp\htdocs\projects\photo_gallery\public\admin\test.php on line 24</strong></p> <p><strong>Fatal error: Call to a member function update() on a non-object in C:\xampp\htdocs\projects\photo_gallery\public\admin\test.php on line 25</strong></p> <p>I have the code broken out into the database.php, user.php and test.php. Any help would be appreciated and to further my learning.</p> <p><strong>database.php</strong></p> <pre><code>require_once(LIB_PATH.DS."db_config.php"); class MySQLDatabase { private $dbh; public $last_query; public $stmt; function __construct() { $this-&gt;open_connection(); } public function open_connection() { try { $this-&gt;dbh = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USER, DB_PASS); $this-&gt;dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $e-&gt;message; exit; } } public function query($query) { $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query); } public function bind($param, $value, $type = null) { if (is_null($type)) { switch (true) { case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break; default: $type = PDO::PARAM_STR; } } $this-&gt;stmt-&gt;bindValue($param, $value, $type); } public function execute() { return $this-&gt;stmt-&gt;execute(); } </code></pre> <p><strong>user.php (extends the database class)</strong></p> <pre><code>require_once(LIB_PATH.DS."database.php"); class User extends MYSQLDatabase { protected static $table_name = "users"; public $id; public $username; public $password; public $first_name; public $last_name; public static function find_by_id($id) { global $database; $result_array = self::find_by_sql("SELECT * FROM " . self::$table_name . " WHERE id = {$id} LIMIT 1"); return $result_array; } public static function find_by_sql($sql = "") { global $database; $result_set = $database-&gt;query($sql); $object_array = array(); $row2 = $database-&gt;resultset(); while ($row = array_shift($row2)) { $object_array[] = self::instantiate($row); echo "&lt;pre&gt;"; print_r($row); echo "&lt;hr /&gt;"; echo "&lt;br /&gt;"; } return $object_array; } private static function instantiate($record) { $object = new self; foreach ($record as $attribute =&gt; $value) { if ($object-&gt;has_attribute($attribute)) { $object-&gt;$attribute = $value; } } return $object; } private function has_attribute($attribute) { // get_object_vars returns an associate array with all attributes // including private ones as the keys and their current values as the value $object_vars = get_object_vars($this); return array_key_exists($attribute, $object_vars); } public function update() { global $database; $sql = "UPDATE users SET username = ?, password = ?, first_name = ?, last_name = ? WHERE id = ?"; $database-&gt;query($sql); $database-&gt;bind(1, $this-&gt;username); $database-&gt;bind(2, $this-&gt;password); $database-&gt;bind(3, $this-&gt;first_name); $database-&gt;bind(4, $this-&gt;last_name); $database-&gt;bind(5, $this-&gt;id); $database-&gt;execute(); return ($database-&gt;lastInsertId() == 1) ? true : false; } </code></pre> <p><strong>test.php</strong></p> <pre><code> &lt;?php require_once("../../includes/initialize.php"); if (!$session-&gt;is_logged_in()) { redirect_to("login.php"); } ?&gt; &lt;?php include_layout_template("admin_header.php"); ?&gt; &lt;?php // Create $user = new User(); // $user-&gt;username = "test01"; // $user-&gt;password = "1234"; // $user-&gt;first_name = "test"; // $user-&gt;last_name = "test"; // $user-&gt;create(); // Update $user-&gt;find_by_id(6); $user-&gt;username = "bingo"; // $user-&gt;password = "123"; // $user-&gt;first_name = "bingo"; // $user-&gt;last_name = "bongo"; // $user-&gt;id = "7"; $user-&gt;update(); ?&gt; &lt;?php include_layout_template("admin_footer.php"); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload