This repository was archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiveData.php
More file actions
122 lines (81 loc) · 2.66 KB
/
Copy pathreceiveData.php
File metadata and controls
122 lines (81 loc) · 2.66 KB
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
<?php
//ini_set("display_errors", 1);
//ini_set("track_errors", 1);
//ini_set("html_errors", 1);
//error_reporting(E_ALL);
$servername = "localhost";
$username = "root";
$password = "xxxxxxxxxxx";
$dbname = "MedicalID";
date_default_timezone_set('US/Eastern');
file_put_contents("data.txt", json_encode($_POST));
if(!isset($_POST["email"]))exit(); //Exit script if we don't even have an email. Lines below won't execute.
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // Initialize the connection.
//$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//$conn->setAttribute(PDO::ERRMODE_EXCEPTION);
// Get current records, just to see if records exists already or not.
$stmt = $conn->prepare("SELECT * FROM users WHERE email=:email");
$stmt->execute(['email' => $_POST["email"]]);
$row = $stmt->fetch();
if(empty($row)){
// Records do not exist, so we will INSERT.
$stmt = $conn->prepare("INSERT INTO users ".createKeyList()." VALUES ".createKeyPreparedList());
$info = array();
foreach($_POST as $key=>$value){
$info[$key] = $value;
//echo "We will bind ".$key." to ".$value."<br>\n";
}
$info["date_last_active"] = date('Y-m-d');
$stmt->execute($info);
echo json_encode(array("success" => "true"));
}else{
//echo "Our updated statement UPDATE users ".createUpdateStmt()." WHERE email=:email<br>";
$stmt = $conn->prepare("UPDATE users SET ".createUpdateStmt()." WHERE email=:email");
$info = array();
foreach($_POST as $key=>$value){
$info[$key] = $value;
//echo "We will bind ".$key." to ".$value."<br>\n";
}
$info["date_last_active"] = date('Y-m-d');
if ($stmt->execute($info)) {
echo json_encode(array("success" => "true"));
exit();
}else{
echo json_encode(array("success" => "false", "c" => "1"));
}
}
} catch (PDOException $e) {
echo $e->getMessage();
}
// INSERT Helpers.
function createKeyList(){
$str = "(";
$keys = array_keys($_POST);
foreach($keys as $key) $str .= $key.", ";
return rtrim($str, ", ").")";
}
function createKeyPreparedList(){
$str = "(";
$keys = array_keys($_POST);
foreach($keys as $key) $str .= ":".$key.", ";
return rtrim($str, ", ").")";
}
function createValueList(){
$str = "(";
$values = array_values($_POST);
foreach($values as $val) $str .= $val.", ";
return rtrim($str, ", ").")";
}
// Update Helpers.
function createUpdateStmt(){
$str = "";
$values = array_keys($_POST);
foreach($values as $val){
if($val == "email")continue;
$str .= $val."=:".$val.", ";
}
return rtrim($str, ", ");
}
?>