Jump to content

php object update query


fife
Go to solution Solved by objnoob,

Recommended Posts

I have the following two classes

 

<?php
  class User {private $userdata = array();  //instantiate The User Classpublic function User(){ }    public function set($var, $value) {$this->userdata[$var] = $value; } public function get($var) {        if(isset($this->userdata[$var]))        {            return $this->userdata[$var];        }        return NULL;    }  function __destruct() {        if($this->userdata){           $this->userdata;        }    }}  class UserService {private $db;private $error;private $fields = array();  public function __construct() {$this->db = new Database(); }//update userpublic function updateUserDetails($user){   $query = sprintf("UPDATE User SET UserForename=%s, UserSurname=%s WHERE IDUser=%s", $this->db->GetSQLValueString($_POST['forename'], "text"), $this->db->GetSQLValueString($_POST['surname'], "text"),$this->db->GetSQLValueString($user->get('IDUser'), "int")); return  $this->db->query($query);  } public function getCurrentUser($fields) { $this->fields = $fields; $query = sprintf("SELECT ".$this->fields." FROM User WHERE IDUser=%s", $this->db->GetSQLValueString($_SESSION['MM_Username'], "int"));$result = $this->db->query($query); if($result && $this->db->num_rows($result) > 0) { //create new user class$user = new User();$row = $this->db->fetch_assoc($result); //set rows as objectforeach($row as $key => $value){$user->set($key, $value); } return $user;}  } 
?>
 

 

My question is as follows.  On my update page Im calling the following;

 

$um = new UserService();
$userfields = "*";
$user = $um->getCurrentUser($userfields);
 
if(isset($_POST['submit'])){
    $um->updateUserDetails($user);
   }
 
 
This runs the update code and returns $this->db->query($query);
 
Now im not too sure how to handle the rest of my update page.  obviously I need a success and fail if statement.  So I tried
 
if(isset($_POST['submit'])){
    $um->updateUserDetails($user);
 
         if(isset($um->db->query($query)){
          header('location : welcome.php');
       } else {
       echo "Error";
      }
 
 
   }
 
but it just says I cant use the $um->db->query() as its a private property.  Im struggling to understand what I've actually returned from the updateUser method.  Ive returned the full query so how do I now use this or make decisions on it?
Link to comment
Share on other sites

  • Solution

You could return true or false from the updateUserDetails method.

 

 

public function updateUserDetails($user){
 
     $sql = sprintf("UPDATE User SET UserForename=%s, UserSurname=%s WHERE IDUser=%s",
 
     $this->db->GetSQLValueString($_POST['forename'], "text"), 
     $this->db->GetSQLValueString($_POST['surname'], "text"),
     $this->db->GetSQLValueString($user->get('IDUser'), "int"));
  
     if($this->db->query($sql) === false) throw new Exception('SQL ERROR!');
     return (bool)$this->db->affected_rows();

}

The call to the method updateUserDetails now returns whether or not the update command changed a record in the database.

If there was an SQL error it throws an exception!

try{
  if($um->updateUserDetails($user)){
    echo 'Your account was updated!';
  }else{
    echo 'Sorry, Your account was not updated!';
  }
}catch(Exception $e){
    echo 'Sorry, there was an SQL Error!';
}

Edited by objnoob
Link to comment
Share on other sites

Ok totally new to object so objnoob thank you for your advice.  I adopted your approach to one I understand.  here is my new code

 

public function updateUserDetails($user){ $sql = sprintf("UPDATE User SET UserForename=%s, UserSurname=%s WHERE IDUser=%s",$this->db->GetSQLValueString($_POST['forename'], "text"), $this->db->GetSQLValueString($_POST['surname'], "text"),$this->db->GetSQLValueString($user->get('IDUser'), "int")); if($this->db->query($sql) === false) {echo mysql_error();} else {      return true;}}   

 

then When Im calling the method

 

if(isset($_POST['submit'])){       if($um->updateUserDetails($user)===true){header('location : /welcome.php?update=true'); } }

 

buuuuut.  it doesnt work.  If the query errors it does show my error.  If the query passes it doesnt run the header redirect and  I dont know why

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.