<?php 

$dir = '../';

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

$errors = array();

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

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

// check id
$sql = "SELECT * FROM listings WHERE id = '{$id}' LIMIT 1";
if(fetch_rows($sql) < 1){
   $errors[] = "Listing #$id";
   $errors[] = "we could not find any records with this ID";
}else{

   $x = fetch_records($sql);

   if($user_id != $x['user_id']){
      $errors[] = "You don't have permission to edit this listing";
   }

}

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

   ex_messages($errors);

}else{

   delete_listing($id);

   ex_messages(['Listing has been deleted successfully']);

}

?>