fife Posted October 27, 2013 Share Posted October 27, 2013 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? Quote Link to comment Share on other sites More sharing options...
phat_hip_prog Posted October 27, 2013 Share Posted October 27, 2013 you are accessing $db directly and its initialized as private, could make an accessor function to execute the query Quote Link to comment Share on other sites More sharing options...
Solution objnoob Posted October 27, 2013 Solution Share Posted October 27, 2013 (edited) 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 October 27, 2013 by objnoob Quote Link to comment Share on other sites More sharing options...
fife Posted October 27, 2013 Author Share Posted October 27, 2013 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 Quote Link to comment Share on other sites More sharing options...
objnoob Posted October 27, 2013 Share Posted October 27, 2013 header('Location: welcome.php?update=true'); Quote Link to comment Share on other sites More sharing options...
fife Posted October 27, 2013 Author Share Posted October 27, 2013 pffft ops. Yes thank you for your time guys my mistake, you rock! 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.