-
Notifications
You must be signed in to change notification settings - Fork 1
/
signUpService.php
77 lines (65 loc) · 2.52 KB
/
signUpService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
// Start the session
session_start();
$email="";
$password="";
$name="";
$city="";
$phone="";
$zip="";
// Set session variables
if ($_SERVER["REQUEST_METHOD"] == "POST") {
# code...
$email=htmlspecialchars($_POST['email']);
$password=htmlspecialchars($_POST['pass']);
$name=htmlspecialchars($_POST['name']);
$city=htmlspecialchars($_POST['city']);
$zip=htmlspecialchars($_POST['zip']);
$phone=htmlspecialchars($_POST['phone']);
$errors = array(); //To store errors
$response = array(); //Pass back the data to `form.php`
require_once "functions/database_functions.php";
$conn = db_connect();
//$conn = mysqli_connect("localhost:8889", "root", "root", "www_project");
// $conn = db_connect();
if(!$conn){
$errors['name'] = "Error in connecting DB";
$response['errors'] = $errors;
echo json_encode($response);
exit();
}
$query = "SELECT user_id_pk FROM user WHERE email = '$email'";
$result = mysqli_query($conn, $query);
$userData = mysqli_fetch_assoc($result);
if($userData['user_id_pk']!=null){
$errors['name'] = 'User is already registered';
$response['success'] = false;
$response['errors'] = $errors;
echo json_encode($response);
exit();
}
$hashPwd = hash('sha256', $password);
$query = "INSERT INTO user (user_id_pk, name, password, email, phone, city, zip, role)
VALUES (NULL, '$name', '$hashPwd', '$email', '$phone', '$city', '$zip', 'user');";
// echo $query;
$result = mysqli_query($conn, $query);
$userData = mysqli_fetch_assoc($result);
//echo "UserData: ".$userData;
if($result == NULL ){
$errors['name'] = 'Error in creating user';
$response['success'] = false;
$response['errors'] = $errors;
echo json_encode($response);
exit();
}else{
$_SESSION["email"] =$email;
$query = "SELECT name,user_id_pk FROM user WHERE email = '$email'";
$result = mysqli_query($conn, $query);
$userData = mysqli_fetch_assoc($result);
$_SESSION["id"]=$userData['user_id_pk'];
$_SESSION["name"]=$userData['name'];
$response['success'] = true;
echo json_encode($response);
}
}
?>