'.$user->userName.' you are now registered
Oh dear. There was an error
'; echo '' . mysqli_error($user->dbc) .'
'; } } /***** students to add code here *****/ /*********END OF CODE TO BE ADDED*****************/ } else{ echo 'Error
'; foreach($errors as $msg) echo " - $msgCould not connect to database
'; return false; } public function save() { if ($this->getDBConnection()) { //escape any special characters $this->firstName = mysqli_real_escape_string($this->dbc, $this->firstName); $this->lastName = mysqli_real_escape_string($this->dbc, $this->lastName); $this->userName = mysqli_real_escape_string($this->dbc, $this->userName); $this->email = mysqli_real_escape_string($this->dbc, $this->email); $this->dob = mysqli_real_escape_string($this->dbc, $this->dob); $this->password = mysqli_real_escape_string($this->dbc, $this->password); $this->image = mysqli_real_escape_string($this->dbc, $this->image); /*if ($this->userName == null) {*/ $q = "INSERT INTO Users_1 (FName, LName, UserName, DOB, Email, Password, Image) values" . "('" . $this->firstName . "','" . $this->lastName . "','" . $this->userName . "', '". $this->dob . "','" . $this->email ."','". $this->password . "','". $this->image ."')"; /*} else { $q = "update Users_1 set FName='" . $this->firstName . "', LName='" . $this->lastName . "',Email='" . $this->email . "', Password='" . $this->password . "' where userName = '" . $this->userName . "'"; }*/ // $q = "call SaveUser2($this->userId,'$this->firstName','$this->lastName','$this->email','$this->password')"; $r = mysqli_query($this->dbc, $q); if (!$r) { $this->displayError($q); return false; } return true; } else { echo 'Could not connect to database
'; return false; } return true; } //end of function public function delete() { if ($this->getDBConnection()) { $q = "DELETE FROM Users_1 WHERE userName=" . mysql_escape_string($this->userName); $r = mysqli_query($this->dbc, $q); if (!$r) { $this->displayError($q); return false; } return true; } else { echo 'Could not connect to database
'; return false; } } public function validateFields() { return $errors; } public function isValid() { //declare array to hold any errors messages $errors = array(); if (empty($this->firstName)) $errors[] = 'You must enter first name'; if (empty($this->userName)) $errors[] = 'You must enter last name'; else { if (!$this->validUserName()) $errors[] = 'This username is already registered'; } if (empty($this->email)) $errors[] = 'You must enter email'; else { if (!$this->validEmail()) $errors[] = 'This email address is already registered'; } if (empty($this->password)) $errors[] = 'You must enter password'; if (empty($this->image)) $errors[] = 'You must enter image path'; return $errors; } public function validEmail() { if ($this->getDBConnection()) { $q = "SELECT userName FROM Users_1 WHERE Email='" . mysqli_escape_string($this->dbc, $this->email) . "'"; $r = mysqli_query($this->dbc, $q); if ($r) { while ($row = mysqli_fetch_array($r)) { $userName = $row[0]; //we have found a record that has this email - if it is not the current user the the email //must be registered to someone else if ($userName != $this->userName) return false; } } else { $this->displayError($q); return false; } } else { echo 'Could not connect to database
'; return false; } return true; } public function validUserName() { if ($this->getDBConnection()) { $q = "SELECT userName FROM Users_1 WHERE userName ='" . mysqli_escape_string($this->dbc, $this->userName) . "'"; $r = mysqli_query($this->dbc, $q); if ($r) { while ($row = mysqli_fetch_array($r)) { $userName = $row[0]; //we have found a record that has this email - if it is not the current user the the email //must be registered to someone else if ($userName != $this->userName) return false; } } else { $this->displayError($q); return false; } } else { echo 'Could not connect to database
'; return false; } return true; } public function getUserFullName() { if ($this->getDBConnection()) { $q = "SELECT CONCAT(FName, ' ', LName) from Users_1 where UserName = $this->userName"; $r = mysqli_query($this->dbc, $q); if($r){ $row = mysqli_fetch_array($r); return $row[0]; } else { $this->displayError($q); return false; } } return false; } private function displayError($q) { echo '' . $q . '
'; echo 'A database error occurred
'; echo '' . mysqli_error($this->dbc) . '
'; } } //end of class decl ?>