-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertdata.php
executable file
·49 lines (43 loc) · 1.55 KB
/
insertdata.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
<?php
include 'credentials.php';
header('Content-type: text/plain');
if( !isset($_POST['username']) || !isset($_POST['passwd']) || !isset($_POST['serial']) ||
!preg_match('/^[A-F0-9]{16}$/',$_POST['serial']) || !isset($_POST['temp']) || !is_numeric($_POST['temp']) ||
!isset($_POST['date']) || !preg_match('/^2[0-9]{3}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/',$_POST['date']) ) {
echo "ERROR: missing or incorrect parameters";
}
else {
$link = new mysqli($dbhost_name, $dbuser, $dbpasswd, $database, $dbport);
if ($link->connect_errno) {
die( "Failed to connect to MySQL: (" . $link->connect_errno . ") " . $link->connect_error);
}
$query = sprintf("select * from users where username='%s' and passwd='%s' and enabled=1",
$link->real_escape_string($_POST['username']),
md5($_POST['passwd']));
$result = $link->query($query);
if (!$result) {
die("ERROR2: query:". $query. ", mysql_error: " . $link->error);
}
$user = $result->fetch_assoc();
mysqli_free_result($result);
if(!$user) {
mysqli_close($link);
die("ERROR3: incorrect authorization!");
}
$query = sprintf("insert into temperature_results(sernum, temp,readdate, user_id) values ('%s',%f,'%s',%d)",
$link->real_escape_string($_POST['serial']),
$link->real_escape_string($_POST['temp']),
$link->real_escape_string($_POST['date']),
$user['id']);
if (!$link->query($query)) {
die("ERROR4:" . $link->error);
}
if($link->affected_rows == 1) {
echo "OK";
}
else {
echo "ERROR?";
}
mysqli_close($link);
}
?>