Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
12juliav authored Sep 10, 2021
1 parent fdd1e23 commit 0335a37
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
47 changes: 47 additions & 0 deletions LAMPAPI/AddContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
$inData = getRequestInfo();

$firstName = $inData["firstName"];
$lastName = $inData["lastName"];
$email = $inData["email"];
$phoneNumber = $inData["phoneNumber"];
$userId = $inData["userId"];

$conn = new mysqli("localhost", "TheManager", "COP4331", "ContactManager");
if ($conn->connect_error)
{
returnWithError( $conn->connect_error );
}
else
{
$sql = "INSERT INTO List (FirstName, LastName, Email, PhoneNumber, UserID)
VALUES ('$firstName', '$lastName', '$email', '$phoneNumber', '$userId' )";

if ($conn->query($sql) === TRUE) {
echo "New contact created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
returnWithError("");
}

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 );
}

?>
48 changes: 48 additions & 0 deletions LAMPAPI/EditContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
$inData = getRequestInfo();

$firstName = $inData["firstName"];
$lastName = $inData["lastName"];
$email = $inData["email"];
$phoneNumber = $inData["phoneNumber"];
$userId = $inData["userId"];
$contactId = $inData["contactId"];

$conn = new mysqli("localhost", "TheManager", "COP4331", "ContactManager");
if ($conn->connect_error)
{
returnWithError( $conn->connect_error );
}
else
{
$sql = "UPDATE List SET firstname= '$firstName', lastname='$lastName', email ='$email', phoneNumber = '$phoneNumber'
WHERE ID = '$contactId' AND UserID = '$userId'";

if ($conn->query($sql) === TRUE) {
echo "Contact update successful";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
returnWithError("");
}

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 );
}

?>
58 changes: 58 additions & 0 deletions LAMPAPI/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

<?php

$inData = getRequestInfo();

$id = 0;
$firstName = "";
$lastName = "";

$conn = new mysqli("localhost", "TheManager", "COP4331", "ContactManager");
if( $conn->connect_error )
{
returnWithError( $conn->connect_error );
}
else
{
$stmt = $conn->prepare("SELECT ID,FirstName,LastName FROM Users WHERE Login=? AND Password =?");
$stmt->bind_param("ss", $inData["login"], $inData["password"]);
$stmt->execute();
$result = $stmt->get_result();

if( $row = $result->fetch_assoc() )
{
returnWithInfo( $row['FirstName'], $row['LastName'], $row['ID'] );
}
else
{
returnWithError("No Records Found");
}

$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 = '{"id":0,"firstName":"","lastName":"","error":"' . $err . '"}';
sendResultInfoAsJson( $retValue );
}

function returnWithInfo( $firstName, $lastName, $id )
{
$retValue = '{"id":' . $id . ',"firstName":"' . $firstName . '","lastName":"' . $lastName . '","error":""}';
sendResultInfoAsJson( $retValue );
}

?>
59 changes: 59 additions & 0 deletions LAMPAPI/ReadContact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

<?php

$inData = getRequestInfo();

$firstName = "";
$lastName = "";
$email = "";
$phoneNumber = 0;

$conn = new mysqli("localhost", "TheManager", "COP4331", "ContactManager");
if( $conn->connect_error )
{
returnWithError( $conn->connect_error );
}
else
{
$stmt = $conn->prepare("SELECT firstName,lastName,email,phoneNumber FROM List WHERE UserID=? AND ID =?");
$stmt->bind_param("ii", $inData["userId"], $inData["contactId"]);
$stmt->execute();
$result = $stmt->get_result();

if( $row = $result->fetch_assoc() )
{
returnWithInfo( $row['firstName'], $row['lastName'], $row['email'], $row['phoneNumber'] );
}
else
{
returnWithError("No Records Found");
}

$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 = '{"firstName":"","lastName":"", "email":"", "phoneNumber":"", "error":"' . $err . '"}';
sendResultInfoAsJson( $retValue );
}

function returnWithInfo( $firstName, $lastName, $email, $phoneNumber )
{
$retValue = '{"firstName":"' . $firstName . '","lastName":"' . $lastName . '", "email":"' . $email . '", "phoneNumber":"' . $phoneNumber . '","error":""}';
sendResultInfoAsJson( $retValue );
}

?>

0 comments on commit 0335a37

Please sign in to comment.