Repository navigation
Expand file tree
/
Copy pathupdate.php
More file actions
97 lines (88 loc) · 2.06 KB
/
Copy pathupdate.php
File metadata and controls
97 lines (88 loc) · 2.06 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
<?php
$inData = getRequestInfo();
$userId = $inData["userId"];
$contactId = $inData["contactId"];
$conn = new mysqli("localhost", "UnNamed", "Small", "COP4331", 3306);
if ($conn->connect_error)
{
returnWithError( $conn->connect_error );
}
else
{
// Build dynamic update query
$fieldsToUpdate = array();
$types = "";
$values = array();
if(isset($inData["firstName"])) {
$fieldsToUpdate[] = "FirstName=?";
$types .= "s";
$values[] = $inData["firstName"];
}
if(isset($inData["lastName"])){
$fieldsToUpdate[] = "LastName=?";
$types .= "s";
$values[] = $inData["lastName"];
}
if(isset($inData["email"])){
$fieldsToUpdate[] = "Email=?";
$types .= "s";
$values[] = $inData["email"];
}
if(isset($inData["phone"])){
$fieldsToUpdate[] = "Phone=?";
$types .= "s";
$values[] = $inData["phone"];
}
if(empty($fieldsToUpdate))
{
returnWithError("No fields to update provided.");
}
else
{
$stmt = $conn->prepare("SELECT COUNT(*) FROM Contacts WHERE UserID=? AND ID=?");
$stmt->bind_param("ii", $userId, $contactId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_row();
if($row[0] == 0)
{
returnWithError("Contact not found.");
}
else
{
$stmt->close();
$sql = "UPDATE Contacts SET " . implode(", ", $fieldsToUpdate) . " WHERE UserID=? AND ID=?";
$values[] = $userId;
$values[] = $contactId;
$types .= "ii";
$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$values);
$stmt->execute();
if($stmt->affected_rows == 1)
{
returnWithError("");
}
else
{
returnWithError("No changes were made.");
}
}
$stmt->close();
}
$conn->close();
}
function getRequestInfo()
{
return json_decode(file_get_contents('php://input'), true);
}
function sendResultInfoAsJson( $obj )
{
header('Content-type: application/json');
echo $obj;
}
function returnWithError($err)
{
$retValue = '{"error":"' . $err . '"}';
sendResultInfoAsJson( $retValue );
}
?>