-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.php
103 lines (79 loc) · 2.94 KB
/
location.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
<?php
// Androidからteacher_idとbeacon_idを受け取りデータベースに日時を記録する
header('Content-Type: text/json; charset=utf-8');
$mysqli = new mysqli("172.18.86.209","labUser","kawa4ken","future_prediction");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$mysqli->query("SET NAMES 'utf8'");
//先生のidとビーコンのidの値 POST
$teacher_id = (int)$_POST["teacher_id"];
$beacon_id = (int)$_POST["beacon_id"];
// 曜日の取得
$weekday = date("l");
// 1.新規の先生/ビーコンの場合,値を挿入して終了
$new_id_check_query = "SELECT COUNT(*) FROM trn_location_info where teacher_id = $teacher_id and beacon_id = $beacon_id";
$result = $mysqli->query($new_id_check_query);
$row = $result->fetch_array();
if (!$row[0]) {
echo "新規\n";
$insert_query = "INSERT INTO `trn_location_info`"
." VALUES(NULL,$teacher_id,$beacon_id,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,'$weekday')";
if(!$mysqli->query($insert_query)){
echo "クエリの実行失敗\n";
}else{
echo "成功\n";
}
exit();
}
// 2.最後に挿入したレコードを判定し 一分以内に挿入されていたならスルー
// $read_last_query = "SELECT * FROM trn_location_info where recode_time = (SELECT MAX(recode_time) FROM trn_location_info) and recode_date = (SELECT MAX(recode_date) FROM trn_location_info) and id = (SELECT MAX(id) FROM trn_location_info)";
$read_last_query = "SELECT * FROM trn_location_info where id = (SELECT MAX(id) FROM trn_location_info)"
." and teacher_id = $teacher_id and beacon_id = $beacon_id";
$last_data = $mysqli->query($read_last_query);
if(!$last_data) {
echo "read_last_query実行失敗";
} else {
$row = $last_data->fetch_array();
$last_date = $row['recode_date'];
$now_date = date("Y-m-d");
$last_time = $row['recode_time'];
$str = str_replace(":","",$last_time);
$db_last = (int) substr( $str , 0 , 4);
$now_time = (int)date("Hi");
print_r($row);
echo "<比較>";
echo strtotime($now_date);
echo "と";
echo strtotime($last_date);
echo "<比較>\n";
//次の日以降ならば必ずinsert
if (strtotime($now_date) > strtotime($last_date)) {
echo "next day\n";
$insert_query = "INSERT INTO `trn_location_info`"
." VALUES(NULL,$teacher_id,$beacon_id,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,'$weekday')";
if(!$mysqli->query($insert_query)){
echo " クエリの実行失敗\n";
}else{
echo " 成功\n";
}
} else {
echo "today\n";
// 直前に挿入したレコードの時間が同じならinsertしない(分単位)
if($now_time > $db_last){
echo "new\n";
$insert_query = "INSERT INTO `trn_location_info`"
." VALUES(NULL,$teacher_id,$beacon_id,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,'$weekday')";
if(!$mysqli->query($insert_query)){
echo "クエリの実行失敗\n";
}else{
echo "成功\n";
}
} else{
echo "old\n";
}
}
}
$mysqli->close();
?>