Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. `<?php
  2.  
  3. require_once '../include/DbOperations.php';
  4.  
  5. $response = array();
  6.  
  7. if($_SERVER['REQUEST_METHOD']=='POST'){
  8. if(
  9. isset($_POST['StudentsEmail']) and
  10. isset($_POST['StudentUsername']) and
  11. isset($_POST['Password']))
  12. {
  13. //operate the data further
  14.  
  15. $db = new DbOperations();
  16.  
  17. $result = $db->createUser( $_POST['StudentsEmail'],
  18. $_POST['StudentUsername'],
  19. $_POST['Password']
  20. );
  21. if($result == 1){
  22. $response['error'] = false;
  23. $response['message'] = "User registered successfully";
  24. }elseif($result == 2){
  25. $response['error'] = true;
  26. $response['message'] = "Some error occurred please try again";
  27. }elseif($result == 0){
  28. $response['error'] = true;
  29. $response['message'] = "It seems you are already registered, please choose a different email and username";
  30. }
  31.  
  32. }else{
  33. $response['error'] = true;
  34. $response['message'] = "Required fields are missing";
  35. }
  36. }else{
  37. $response['error'] = true;
  38. $response['message'] = "Invalid Request";
  39. }
  40.  
  41. echo json_encode($response);`
  42.  
  43. <?php
  44.  
  45. class DbOperations{
  46.  
  47. private $con;
  48.  
  49. function __construct(){
  50.  
  51. require_once dirname(__FILE__).'/DbConnect.php';
  52.  
  53. $db = new DbConnect();
  54.  
  55. $this->con = $db->connect();
  56.  
  57. }
  58.  
  59. /*CRUD -> C -> CREATE */
  60.  
  61. public function createUser($studemail,$studusername,$studpass){
  62. if($this->isUserExist($studemail,$studusername)){
  63. return 0;
  64. }else{
  65. //$password = md5($studpass);
  66. $stmt = $this->con->prepare("INSERT INTO `studentdetails` (`Id`, `StudentFullName`, `Gender`, `Age`, `NationalIdentification`, `RegistrationNumber`, `StudentsEmail`, `StudentUsername`, `Password`, `PhoneNumber`, `YearofStudy`, `Semester`, `StudentImage`) VALUES (NULL,NULL, NULL,NULL,NULL,NULL,?,?,?,NULL,NULL,NULL,NULL);");
  67. //$stmt->bind_param("sss",$studemail,$studusername,$password);
  68. $stmt->bind_param("sss",$studemail,$studusername,$studpass);
  69.  
  70. if($stmt->execute()){
  71. return 1;
  72. }else{
  73. return 2;
  74. }
  75. }
  76. }
  77.  
  78. public function userLogin($studusername, $studpass){
  79. //$password = md5($studpass);
  80. $stmt = $this->con->prepare("SELECT Id FROM studentdetails WHERE StudentUsername = ? AND Password = ?");
  81. //$stmt->bind_param("ss",$studusername,$password);
  82. $stmt->bind_param("ss",$studusername,$studpass);
  83. $stmt->execute();
  84. $stmt->store_result();
  85. return $stmt->num_rows > 0;
  86. }
  87.  
  88. public function getUserByUsername($studusername){
  89. $stmt = $this->con->prepare("SELECT * FROM studentdetails WHERE StudentUsername = ?");
  90. $stmt->bind_param("s",$studusername);
  91. $stmt->execute();
  92. return $stmt->get_result()->fetch_assoc();
  93. }
  94.  
  95.  
  96. public function isUserExist($studemail, $studusername){
  97. $stmt = $this->con->prepare("SELECT Id FROM studentdetails WHERE StudentsEmail = ? OR StudentUsername = ?");
  98. $stmt->bind_param("ss", $studemail, $studusername);
  99. $stmt->execute();
  100. $stmt->store_result();
  101. return $stmt->num_rows > 0;
  102. }
  103. }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement