Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `<?php
- require_once '../include/DbOperations.php';
- $response = array();
- if($_SERVER['REQUEST_METHOD']=='POST'){
- if(
- isset($_POST['StudentsEmail']) and
- isset($_POST['StudentUsername']) and
- isset($_POST['Password']))
- {
- //operate the data further
- $db = new DbOperations();
- $result = $db->createUser( $_POST['StudentsEmail'],
- $_POST['StudentUsername'],
- $_POST['Password']
- );
- if($result == 1){
- $response['error'] = false;
- $response['message'] = "User registered successfully";
- }elseif($result == 2){
- $response['error'] = true;
- $response['message'] = "Some error occurred please try again";
- }elseif($result == 0){
- $response['error'] = true;
- $response['message'] = "It seems you are already registered, please choose a different email and username";
- }
- }else{
- $response['error'] = true;
- $response['message'] = "Required fields are missing";
- }
- }else{
- $response['error'] = true;
- $response['message'] = "Invalid Request";
- }
- echo json_encode($response);`
- <?php
- class DbOperations{
- private $con;
- function __construct(){
- require_once dirname(__FILE__).'/DbConnect.php';
- $db = new DbConnect();
- $this->con = $db->connect();
- }
- /*CRUD -> C -> CREATE */
- public function createUser($studemail,$studusername,$studpass){
- if($this->isUserExist($studemail,$studusername)){
- return 0;
- }else{
- //$password = md5($studpass);
- $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);");
- //$stmt->bind_param("sss",$studemail,$studusername,$password);
- $stmt->bind_param("sss",$studemail,$studusername,$studpass);
- if($stmt->execute()){
- return 1;
- }else{
- return 2;
- }
- }
- }
- public function userLogin($studusername, $studpass){
- //$password = md5($studpass);
- $stmt = $this->con->prepare("SELECT Id FROM studentdetails WHERE StudentUsername = ? AND Password = ?");
- //$stmt->bind_param("ss",$studusername,$password);
- $stmt->bind_param("ss",$studusername,$studpass);
- $stmt->execute();
- $stmt->store_result();
- return $stmt->num_rows > 0;
- }
- public function getUserByUsername($studusername){
- $stmt = $this->con->prepare("SELECT * FROM studentdetails WHERE StudentUsername = ?");
- $stmt->bind_param("s",$studusername);
- $stmt->execute();
- return $stmt->get_result()->fetch_assoc();
- }
- public function isUserExist($studemail, $studusername){
- $stmt = $this->con->prepare("SELECT Id FROM studentdetails WHERE StudentsEmail = ? OR StudentUsername = ?");
- $stmt->bind_param("ss", $studemail, $studusername);
- $stmt->execute();
- $stmt->store_result();
- return $stmt->num_rows > 0;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement