<?php
	session_start();
	include"../../connect/db.php";

 	if( isset($_POST['rbank']) ){

 		$rbank   = mysqli_real_escape_string($con, $_POST['rbank']);
 		$accno   = mysqli_real_escape_string($con, $_POST['accno']);
 		$rname   = mysqli_real_escape_string($con, $_POST['rname']);
 		$routing = isset($_POST['routing']) ? mysqli_real_escape_string($con, $_POST['routing']) : '';
 		$chap    = $_SESSION['chap'];

 		$sql = mysqli_query($con, "SELECT * FROM beneficiary WHERE user = '$chap' AND account_num = '$accno'");

 		if( mysqli_num_rows($sql) == 0 ){
 			$kay = mysqli_query($con, "INSERT INTO beneficiary (name, account_num, bank, routing, user) VALUES ('$rname', '$accno', '$rbank', '$routing', '$chap')");
 			if( $kay ){
 				echo "success";
 			}
 			else{
 				echo "error";
 			}
 		}
 		else{
 			// Update existing beneficiary with latest details
 			mysqli_query($con, "UPDATE beneficiary SET name='$rname', bank='$rbank', routing='$routing' WHERE user='$chap' AND account_num='$accno'");
 			echo "updated";
 		}
 	}
 	else{
 		echo"error";
 	}
?>