 <?php
   
    $Offer='';
    $Offer_Code='';
    $Web_Entry_No=0;
    $Start_Date='';
    $End_Date='';
    $comp_id='';
    $mess="Error";
    if (isset($_POST["Offer"])){
         $Offer=$_POST["Offer"];
    }
    else {
        die("No offer data");
    }
    if (isset($_POST["Offer_Code"])){
        $Offer_Code=$_POST["Offer_Code"];
    }
    else {
        die("No offer code");
    }
    if (isset($_POST["Web_Entry_No"])){
        $Web_Entry_No=$_POST["Web_Entry_No"];
    }
    else {
        die("No Web Entry no");
    }
    if (isset($_POST["Start_Date"])){
        $Start_Date=$_POST["Start_Date"];
    }
    else {
         die("No start date");
    }
    if (isset($_POST["End_Date"])){
         $End_Date=$_POST["End_Date"];
    }
    else {
         die("No end date");
    }
    if (isset($_POST["comp_id"])){
         $comp_id=$_POST["comp_id"];
    }
    else {
         die("No company id ");
    }
    if($comp_id==''){
        die("Invalid company id "); 
    }
   
   
    include_once('opendb.php');
    if ($Web_Entry_No==0){
        $sql = "INSERT INTO web_offers (offer_code,offer_desc,start_date,end_date,comp_id) VALUES ('$Offer_Code','$Offer','$Start_Date','$End_Date',$comp_id)";
        if ($conn->query($sql) === TRUE)
        {
           $Web_Entry_No = $conn->insert_id;
           $mess=$Web_Entry_No;
        } else
        {
           die( "Error: Inerting offer data " . $sql . "<br>" . $conn->error);
        } 
    }
    else {
        $sql = "UPDATE web_offers SET offer_code='".$Offer_Code."',offer_desc='".$Offer."',start_date='".$Start_Date."',end_date='".$End_Date."' WHERE comp_id=" .$comp_id . " and web_entry_no=". $Web_Entry_No ;
        if ($conn->query($sql) === TRUE) {
            $mess=$Web_Entry_No;
        }
         else
        {
           die( "Error: updating offer data " . $sql . "<br>" . $conn->error);
        } 
        
    }
          
    echo $mess;
    $conn->close();
?>