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

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'])) {
    print_r($_POST);
    global $wpdb;
    $table_name = $wpdb->prefix . 'hosting_companies';
    $res = $_POST['results_HC'];
    $del = $_POST['deleted_HC'];
    $ind = 0;
    if($del){
        foreach ($del as $delt){
            $wpdb->delete($table_name, array('id' => $delt));
        }
    }
    foreach ($res as $resu) {
        $id = $resu['id'];
        echo $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'],
            ));
        }
    }
    $location = $_SERVER['HTTP_REFERER'];
    wp_safe_redirect($location);
}

