eldan88 Posted April 18, 2013 Share Posted April 18, 2013 Hey, I ran into an issue with updating a record through a class. Everytime I try to run the update i get the following error message? Fatal error: Call to undefined method stdClass::update() in /Applications/XAMPP/xamppfiles/htdocs/photo_gallery/public/admin/test.php on line 20 below is the code I am using to preform the update. $user = User::find_by_id(2); $user->password = "123123123"; $user->update(); and here is the method that I am calling Class User { public function update() { global $database; $sql = "UPDATE users SET username = '{$this->username}', password = '{$this->password}', first_name = '{$this->first_name}', last_name = '{$this->last_name}' WHERE id = {$this->id} "; $database->query($sql); return ($database->affected_rows() == 1) ? true : false; } } Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 18, 2013 Share Posted April 18, 2013 Does find_by_id() return a User object? Quote Link to comment Share on other sites More sharing options...
eldan88 Posted April 18, 2013 Author Share Posted April 18, 2013 Does find_by_id() return a User object? Yes sorry. Its part of my user method I forgot to include it. This is how it looks. public static function find_by_id($id=0) { // Takes an ID as an argument $result_array = self::find_by_sql("SELECT * FROM users WHERE id={$id} LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 18, 2013 Share Posted April 18, 2013 Is find_by_sql() returning a User object? It doesn't look like it since you are calling array_shift() on it, which is also confusing. Using array_shift() like that will just return the first value of the array. Is that your intention? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 19, 2013 Share Posted April 19, 2013 your code doesn't produce that fatal error for me for an id that exists (after coping the code together from all the snippets the ONE class is posted as). when the id doesn't exist the query doesn't match any rows and your code returns a false value instead of an instance of the user class. you ALWAYS, not just for debugging but ALWAYS, need to have logic in your code to check if any step worked (produced the result you expect) before trying to use the result from that step. you also don't have php's error reporting set to E_ALL. there is warning about "Creating default object from empty value" at the ->password = ''; line. you are working on ONE class. when you have a problem with the code in that class, why not just post the whole class so that someone could see (or test) all the code needed in one place? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.