-
Notifications
You must be signed in to change notification settings - Fork 0
/
appi.php
123 lines (105 loc) · 4.1 KB
/
appi.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
$hostname = "localhost";
$username = "tiisor_waoUser";
$password = "Nuv)wX!6IBKY";
$database = "tiisor_wao";
$conn = new mysqli($hostname,$username,$password, $database);
$response = array();
if(isset($_GET['action'])) {
switch($_GET['action']){
//Sajili malalamiko
case 'sajili':
if(isValid(array('kero', 'ngazi'))) {
$kero = $_POST['kero'];
$ngazi = $_POST['ngazi'];
$namba = $_POST['namba'];
$stmt = $conn->prepare("SELECT * FROM db_users1 WHERE username = ? OR email = ?");
$stmt->bind_param("ss", $namba, $ngazi);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows == 0) {
//if user is new creating an insert
$stmt = $conn->prepare("INSERT INTO db_users1 (username, email, password) VALUES (?, ?, ?)");
//$stmt = $conn->prepare("SELECT id FROM db_users1 WHERE username = ? OR email = ?");
//$stmt->bind_param("ss", $namba, $ngazi);
$stmt->bind_param("sss", $kero, $ngazi, $namba);
//if the user is successfully added to the database
if($stmt->execute()){
$malalamiko = array(
'kero'=>$kero,
'ngazi'=>$ngazi,
'namba'=>$namba
);
//adding the user data in response
$response['error'] = false;
$response['message'] = 'Malalamiko yako yamepokelewa, utatumiwa namba ya malalamiko si muda mrefu.';
$response['malalamiko'] = $malalamiko;
} else {
$response['error'] = true;
$response['message'] = 'Malalamiko yako hayakupokelewa.';
}
} else {
$response['error'] = true;
$response['message'] = 'Malalamiko yalishapokelewa zamani.';
}
} else {
$response['error'] = true;
$response['message'] = 'Malalamiko hayakukamilika.';
}
break;
case 'fuatilia':
if(isValid(array('Namba'))){
//getting values
$Namba = $_POST['Namba'];
//creating the check query
$stmt = $conn->prepare("SELECT `kero_number`, `description`, `status` FROM `kero` WHERE kero_number=?");
$stmt->bind_param("s",$Namba);
$stmt->execute();
$stmt->store_result();
//if the user exist with given credentials
if($stmt->num_rows > 0) {
$stmt->bind_result($kero_no, $desc, $status);
$stmt->fetch();
$ombi = array(
'namba'=>$kero_no,
'kero'=>$desc,
'status'=>$status
);
$response['error'] = false;
$response['message'] = 'Ombi lako linafanyiwa.';
$response['Ombi'] = $ombi;
}else{
//if the user not found
$response['error'] = true;
$response['message'] = 'Malalamiko ya namba hiyo hayapo.';
}
} else {
$response['error'] = true;
$response['message'] = 'Umekosea namba ya malalamiko .';
}
break;
default;
$response['error'] = true;
$response['message'] = 'Invalid Action.';
break;
}
} else {
$response['error'] = true;
$response['message'] = 'Invalid Request.';
}
function isValid($params){
foreach($params as $param) {
//if the paramter is not available or empty
if(isset($_POST[$param])) {
if(empty($_POST[$param])){
return false;
}
} else {
return false;
}
}
//return true if every param is available and not empty
return true;
}
echo json_encode($response);
?>