<?php
$path = $_SERVER['DOCUMENT_ROOT'];

include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

if (isset($_POST['submit'])) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'hosting_companies';
    $res = $_POST['results_HC'];
    $del = $_POST['deleted_HC'];
    $ind = 0;
    if ($del) {
        foreach ($del as $delt) {
            if ($wpdb->get_var('SELECT id FROM ' . $table_name . ' WHERE id = ' . $delt)) {
                $wpdb->delete($table_name, array('id' => $delt));
            }
        }
    }
    if($res){
        foreach ($res as $resu) {
            $id = $resu['id'];
            $item = $wpdb->get_var('SELECT id FROM ' . $table_name . ' WHERE id = ' . $id);
            if ($item) {
                $success = $wpdb->update($table_name, $resu, array('id' => $resu['id']));
            } else {
                $success = $wpdb->insert($table_name, array(
                    "id" => $resu['id'],
                    "name" => $resu['name'],
                    "legal_name" => $resu['legal_name'],
                    "APLT" => 0,
                    "ART" => 0
                ));
            }
        }
    }
    $location = $_SERVER['HTTP_REFERER'];
    wp_safe_redirect($location);
}

