<?php
include('database.php');
session_start();


if(isset($_POST['submit']))
{
	$passcode = mysqli_real_escape_string($connection, $_POST['passcode']);
	$newpasscode = mysqli_real_escape_string($connection,  $_POST['newpasscode']);
	$id =  mysqli_real_escape_string($connection, $_POST['id']);
	if ($stmt = $connection->prepare('SELECT code FROM users WHERE id =?')){
	$stmt->bind_param('i', $_POST['id']);
	$stmt->execute();
	// Store the result so we can check if the account exists in the database.
	$stmt->store_result();
	}
	if($stmt->num_rows > 0) {
	$stmt->bind_result($oldpasscode);
	$stmt->fetch();
	if ($passcode == $oldpasscode)
	{
	
    $query ="UPDATE users SET code='$newpasscode' WHERE id='$id'";
    $query_run = mysqli_query($connection, $query);
    if($query_run)
    {
		$_SESSION['status'] = "<h5 style='padding:8px; background: #1a1849; border-radius: 10px; color: #fff; text-align: center;'>PASSCODE UPDATED SUCCESSFULLY</h5>";
		header('location:../settings.php');
	}
    else
    {
		$_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>error.. passcode not </h5>";
		header('location:../settings.php');
	}		
}else {
	$_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>INCORRECT CURRENT PASSCODE</h5>";
	header('location:../settings.php');	
}
}else{
	$_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>passcode not updated</h5>";
	header('location:../settings.php');	
}
}
else{
	$_SESSION['status'] = "<h5 style='padding:8px; background: #fe8b8e; border-radius: 10px; color: #fff; text-align: center;'>error.. passcode not updated</h5>";
	header('location:../settings.php');	
}

?>