<?php
if (isset($_POST["action"])) {
	$output = '';
	require('../config/connection.php');
	if ($_POST["action"] == "Add") {
		$query = "SELECT srno from control_mst where description='SEGMENT'";
		$rs = mysqli_query($con, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
		}
		$pilgrim_name = $_POST["pilgrim_name"];
		$age = mysqli_escape_string($con, $_POST["age"]);
		$gender = mysqli_escape_string($con, $_POST["gender"]);
		$email = mysqli_escape_string($con, $_POST["email"]);
		$mobile_number = mysqli_escape_string($con, $_POST["mobile_number"]);
		$aadhaar_number = mysqli_escape_string($con, $_POST["aadhaar_number"]);
		$place = mysqli_escape_string($con, $_POST["place"]);
		$query = "insert into profile_mst(id,pilgrim_name,age,gender, email,mobile_number,aadhaar_number,place) values ('" . $srno . "','" . $pilgrim_name . "','" . $age . "','" . $gender . "','" . $email . "','" . $mobile_number . "','" . $aadhaar_number . "','" . $place . "')";
		if (mysqli_query($con, $query)) {
			$update = "update control_mst set srno = '" . $srnoupd . "' where description='SEGMENT'";
			mysqli_query($con, $update) or die(mysqli_error());
		}
	}

	if ($_POST["action"] == "Update") {
		$pilgrim_name = $_POST["pilgrim_name"];
		$age = mysqli_escape_string($con, $_POST["age"]);
		$gender = mysqli_escape_string($con, $_POST["gender"]);
		$email = mysqli_escape_string($con, $_POST["email"]);
		$mobile_number = mysqli_escape_string($con, $_POST["mobile_number"]);
		$aadhaar_number = mysqli_escape_string($con, $_POST["aadhaar_number"]);
		$place = mysqli_escape_string($con, $_POST["place"]);
		$id = mysqli_escape_string($con, $_POST["id"]);
		$update = "update profile_mst set pilgrim_name = '" . $pilgrim_name . "',age = '" . $age . "',gender = '" . $gender . "',email = '" . $email . "',mobile_number = '" . $mobile_number . "',aadhaar_number = '" . $aadhaar_number . "',place = '" . $place . "' WHERE id = '" . $id . "'";
		if (mysqli_query($con, $update)) {
		}
	}
	if ($_POST["action"] == "Delete") {
		$delete = "DELETE FROM profile_mst WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($con, $delete)) {
		    
		}
	}
}