Jump to content

Question about updating a record


eldan88

Recommended Posts

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;
   
   }
}
Link to comment
Share on other sites

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;
  }
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.