<?php 

$dir = '../';

require_once $dir.'includes/main.php';

$errors = array();

$id = (int)$_GET['id'];

// check login
if(!isset($user_id)){
   $errors[] = LOG_IN;
}

// check id
$sql = "SELECT * FROM orders WHERE id = '{$id}' LIMIT 1";
if(fetch_rows($sql) < 1){
   $errors[] = "There is no such order";
}else{

   $x = fetch_records($sql);

   if($user_id != $x['user_id']){
      $errors[] = "You do not have authorization to cancel this order";
   }

}

// check errors
if(!empty($errors)){

   $_SESSION['messages'] = $errors;

}else{

   query("UPDATE orders SET status = 'Canceled' WHERE id = '{$id}' LIMIT 1");

   $_SESSION['messages'][] = "Order has been canceled successfully";

}

header('location: ../dashboard/orders');
exit;

?>