<?php

    error_reporting(-1);
    ini_set('display_errors', 'On');
    
    $row                =   array();
    $row['id_offer']    =   31;
    
    
    if( ($_GET["id_list"] > 0) && ($_GET["id_user"] > 0) && ($row['id_offer']>0) )
    {
        $id_list    =   $_GET["id_list"];
        $id_user    =   $_GET["id_user"];
        $id_offer   =   $row['id_offer'];

        //echo $id_list."<br/>".$id_user."<br/>".$id_offer;
        //exit;

        include('../scripts/bdd.php');


        //1- Get List Infos :
        $sqlTableInfos  =
        "
            SELECT  data_table 
            FROM    data_list 
            WHERE   id  =   ?
        ";

        $cmdTableInfos  =   $bdd->prepare($sqlTableInfos);
        $cmdTableInfos->execute(array($id_list));
        $res            =   $cmdTableInfos->fetch();
        $tableName      =   $res['data_table'];

        //echo "Le nom de la table est : ".$tableName;
        //exit;



        //2- Get Email Infos :
        $sqlEmailInfos  =
        "
            SELECT  *
            FROM    $tableName
            WHERE   id  =   ?
        ";
        $cmdEmailInfos  =   $bdd->prepare($sqlEmailInfos);
        $cmdEmailInfos->execute(array($id_user));
        $user            =   $cmdEmailInfos->fetch();


        
        //var_dump($user);
        //exit;
        
        

        //3- Get the vertical click table name :
        $sqlVerticalClickTable      =
        "
            SELECT  name_click_table
            FROM    vertical
            WHERE   id_vertical     =   (select id_vertical from offer where id = ?)
        ";
        $cmdVerticalClickTable      =   $bdd->prepare($sqlVerticalClickTable);
        $cmdVerticalClickTable->execute(array($id_offer));
        $vertical_row               =   $cmdVerticalClickTable->fetch();
        $verticalClickTableName     =   $vertical_row['name_click_table'];
        
        echo "Le nom de la table de click du vertical est : ".$verticalClickTableName;
        exit;



        //4- Save in the vertical table :
        
        if($user && !empty($verticalClickTableName))
        {
            $uniq_id        =   $user['unique_id'];
            $email_address  =   $user['email_address'];
            $email_md5      =   $user['email_md5'];

            $sqlInsertUser  =
            "
                INSERT  INTO    $verticalClickTableName     VALUES (NULL,?,?,?);
            ";
            $cmdInsertUser  =   $bdd_data->prepare($sqlInsertUser);
            $cmdInsertUser->execute(array($uniq_id,$email_address,$email_md5));
        }
        
    }

