Connection.php
<?php
// Create connection
$conn = mysqli_connect("localhost","root" , "","testdb");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
---
Index.php
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
include "Connection.php";
$json = file_get_contents("php://input");
$obj= json_decode($json,true);
$mark= $obj["mark"];
if ($mark==1)
{
$result=mysqli_query($conn,"Select * from emp");
if (mysqli_num_rows($result) > 0) {
while($row[] = mysqli_fetch_assoc($result)) {
$data=json_encode($row);
}
echo $data;
}
else
{
echo json_encode("result not found.");
}
}
elseif($mark==2)
{
$eid=$obj["eid"];
$result=mysqli_query($conn,"Select * from emp where EID='$eid'");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$data=json_encode($row);
}
echo $data;
}
else
{
echo json_encode("result not found.");
}
}
elseif($mark==3)
{
$name=$obj["name"];
$email=$obj["email"];
$gender=$obj["gender"];
$result=mysqli_query($conn,"INSERT INTO emp (`NAME`, `EMAIL`, `GENDER`) VALUES ('$name', '$email','$gender')");
if($result)
{
echo json_encode("Data Inserted successfully.");
}
else
{
echo json_encode("Something went wrong.");
}
}
elseif($mark==4)
{
$eid=$obj["eid"];
$name=$obj["name"];
$email=$obj["email"];
$gender=$obj["gender"];
$result=mysqli_query($conn,"update emp set NAME='$name',EMAIL='$email',GENDER='$gender' where EID='$eid'");
if($result)
{
echo json_encode("Data updated successfully.");
}
else
{
echo json_encode("Something went wrong.");
}
}
elseif($mark==5)
{
$eid=$obj["eid"];
$result=mysqli_query($conn,"delete from emp where EID='$eid'");
if($result)
{
echo json_encode("Data deleted successfully.");
}
else
{
echo json_encode("Something went wrong.");
}
}
mysqli_close($conn);
?>
No comments:
Post a Comment