Jump to content

php object update query


fife

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
https://forums.phpfreaks.com/topic/283348-php-object-update-query/
Share on other sites

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!';
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.