<?php
$host = "localhost";
$user = "colin";
$pw = "CmA112209";
$db = "DITLLogin";
$con = mysqli_connect($host,$user,$pw) or die(mysqli_error());
mysqli_select_db($con,$db) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
mysqli_query($con,"SET NAMES 'utf8'");
$json = file_get_contents("php://input");
$jsall = array();
$jsone = array();
$jsall=json_decode($json, true);
$jsone=$jsall[0];
$action = $jsone["Action"];
switch ($action)
{
Case "InsertPerson":
$pname=stripslashes(mysqli_real_escape_string($con,$jsone["name"]));
$pphone=stripslashes(mysqli_real_escape_string($con,$jsone["phone"]));
$pparty=stripslashes(mysqli_real_escape_string($con,$jsone["party"]));
$stmt = $con->prepare("INSERT INTO persons (pname, pphone, pparty) VALUES (?, ?, ?)");
$rc=$stmt->bind_param("sss", $pname, $pphone, $pparty);
$rc=$stmt->execute();
$iid=$stmt->insert_id;
if ($iid == 0)
{
echo json_encode(array(array('InsertPerson' => 'failed')));
exit (-1);
}
else
{
echo json_encode(array(array('InsertPerson' => 'ok', 'pid' => $iid)));
exit (0);
}
break;
Case "DeletePerson":
$pid=stripslashes(mysqli_real_escape_string($con,$jsone["pid"]));
$stmt = $con->prepare("DELETE FROM persons WHERE pid = ?");
$rc=$stmt->bind_param("i", $pid);
$rc=$stmt->execute();
$ar=$stmt->affected_rows;
if ($ar < 1)
{
echo json_encode(array(array('DeletePerson' => 'deletenorow')));
exit(-2);
}
else
{
echo json_encode(array(array('DeletePerson' => 'deleteok')));
exit(0);
}
break;
Case "GetAllPersons":
$stmt = $con->prepare("SELECT * from login ORDER BY pid ASC");
//$rc=$stmt->bind_param("s", $uname);
$rc=$stmt->execute();
$q = $stmt->get_result();
$count=mysqli_num_rows($q);
$stmt->close();
$rows = array();
while($r = mysqli_fetch_assoc($q))
{
$rows[] = $r;
}
//exit (json_encode(array(array('persons' => 'ok', 'personlist' => json_encode($rows)))));
echo json_encode($rows);
exit(0);
break;
default:
print json_encode ("Error: Function not defined (" . $action . ")");
}
?>