<?
$email = $_GET['email'];
$name = "Albertini";
$intnumber = "0508965889";


$geo = $resultneutrinoCheckPhone->countryCode;
            $intnumber = $resultneutrinoCheckPhone->internationalNumber;
            $type = $resultneutrinoCheckPhone->type;
            $info['sid'] = $_GET['affid'];
            $info['cid'] = $_GET['tag'];
            $info['lp']  = "1";
            $info['sub1'] = $_GET['sub1'];
            $info['sub2'] = $_GET['sub2'];
            $info['ref']  = $_GET['ref'];
            $userid = saveToDB($email, $name, $intnumber, $geo, $type, $info);

// **** DATABASE ****
function saveToDB($email, $name, $phone, $geo, $type, $info)
{


    $servername = "localhost";
    $database = "bitcoint_leads";
    $username = "bitcoint_lesduse";
    $password = 'BVK,_ozN@$Zs';

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $database);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $exist = FALSE;

    $sql = "SELECT email FROM ella WHERE email='$email' LIMIT 1";
    $result = mysqli_query($conn, $sql);

    $exist = mysqli_num_rows($result) > 0;


    if (!$exist) {
		
        $ip   = $_SERVER['REMOTE_ADDR'];
		//$geo  = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
		$cid  = $info['cid'];
		$sid  = $info['sid'];
		$lp   = $info['lp'];
		$sub1 = $info['sub1'];
		$sub2 = $info['sub2'];
		$ref  = $info['ref'];
		$t = time();

        $sql = "INSERT INTO ella (date,email,name,phone,ip,geo,type,refid,lp,sid,cid,sub1,sub2) VALUES ('$t','$email','$name','$phone','$ip','$geo','$type','$ref','$lp','$sid','$cid','$sub1','$sub2')";
        if (!mysqli_query($conn, $sql)) {
            $response = array(
                'success' => false,
                'error' => $sql . "<br>" . mysqli_error($conn),
            );
            http_response_code(500);
            echo json_encode($response);
			$last_id = $conn->insert_id;
			return $last_id;
        }
	//	$leadstatus = sendlead($email,$name,$phone,$geo);
    } else {

        /*$sql = "UPDATE data SET count=count+1 WHERE email='$email'";

        if (!mysqli_query($conn, $sql)){
            $response = array(
                'success' => false,
                'error' => $sql . "<br>" . mysqli_error($conn),
            );
            http_response_code(500);
            echo json_encode($response);
        }*/
    }

    mysqli_close($conn);
}
