<?php
session_start();
require_once("config/configDB.php"); 

function validation_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $refcode   = stripslashes($_POST['refcode']);
    $rate_date        = stripslashes($_POST['rate_date']);
    $gold_rate   = stripslashes($_POST['gold_rate']);
    $silver_rate         = stripslashes($_POST['silver_rate']);

$old_gold_rate   = stripslashes($_POST['old_gold_rate']);
    $old_silver_rate         = stripslashes($_POST['old_silver_rate']);

    $createdate  	= date("Y-m-d");
    $createdby      = $_SESSION['username'];
    $userID         = $_SESSION['user_id'];
	
	$sts=mysqli_query($conn,"update gold_rate set status='deleted' where status='' ");
	
	$autoinc1=mysqli_query($conn,"ALTER TABLE gold_rate CHANGE id id INT(11) NOT NULL AUTO_INCREMENT ");

		$count=0;
$check_query	= mysqli_query($conn,"SELECT count(refcode) as counts FROM gold_rate WHERE ecode = '$employeecode' where status='' "); // Check the database


while($row = mysqli_fetch_array($check_query)){
$count=$row['counts'];
}
if($count!=0){
// Already Exsist


		echo "<script> alert ('Data Inserted Success.');</script>";
		?>
<script language="javascript">window.location.href = "rate.php";</script>
<?php
		

}		
else
{

    $sql  = "INSERT INTO gold_rate ( refcode, rate_date, gold_rate, silver_rate,old_gold_rate, old_silver_rate, isdelete,createdby,userID,createdate,status) VALUES ('$refcode', '$rate_date',    '$gold_rate', '$silver_rate','$old_gold_rate', '$old_silver_rate', '1','".$createdby."','".$userID."','".$createdate."','')";
   

    mysqli_query($conn,$sql);

    $employeeID = mysqli_insert_id($conn);
    
    header("Location:rate.php");

}

}



?>