-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeDatabase.php
48 lines (40 loc) · 1.14 KB
/
writeDatabase.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
<?php
$servername = "localhost";
$username = "root";
$password = "emidio";
$dbname = "alarmSystem";
$uid = $_GET['uid'];
$token = $_GET['token'];
$off = $_GET['off'];
$sensor = $_GET['sensor'];
$zero = "0";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//First check
$check = "SELECT COUNT(*) FROM data WHERE uid='{$uid}'";
$result = mysqli_query($conn,$check);
$row_for_1_check=mysqli_fetch_row($result);
printf("%s \n", $row_for_1_check[0]);
$num_rows = (string)$row_for_1_check[0];
settype($zero, "string");
settype($sensor, "string");
if($sensor=='0'){
if($num_rows==$zero){
$sql = "INSERT INTO data (uid, token, off) VALUES ('$uid', '$token', '$off')";
}else{
$sql = "UPDATE data SET token = '{$token}' WHERE uid = '{$uid}'";
}
}else{
$sql = "UPDATE data SET off='{$off}' WHERE uid = '{$uid}'";
}
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>