<?php
session_start();
include '../../assets/constant/config.php';

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if (isset($_POST['submit'])) {


        $stmt = $conn->prepare("INSERT INTO `campaign`(`cam_name`,`goal`,`sdate`, `edate`,`notes`,`status`) VALUES (?,?,?,?,?,?)");

        $stmt->execute([
           
            htmlspecialchars($_POST['cam_name'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['goal'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['sdate'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['edate'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['notes'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['status'], ENT_QUOTES, 'UTF-8'),
           
        ]);

        $_SESSION['success'] = "success";

        header("location:../manage_campaign.php");
    }

    



// update campaign....

    if (isset($_POST['update'])) {


      
        // Prepare and execute the SQL update statement
        $stmt = $conn->prepare("UPDATE `campaign` SET `cam_name`=?,`goal`=?,`sdate`=?,`edate`=?,`notes`=?, `status`=? WHERE id=? ");
                  $stmt->execute([
            htmlspecialchars($_POST['cam_name'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['goal'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['sdate'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['edate'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['notes'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['status'], ENT_QUOTES, 'UTF-8'),
          
        htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8')
        ]);
    
        $_SESSION['update'] = "update";
        header("location:../manage_campaign.php");
    }
    

    // delete member..

    if (isset($_POST['del_id'])) {

        $stmt = $conn->prepare("UPDATE `campaign` SET delete_status='1' where id=? ");

        $stmt->execute([htmlspecialchars($_POST['del_id'], ENT_QUOTES, 'UTF-8')]);

        $_SESSION['delete'] = "delete";

        header("location:../manage_campaign.php");
    }
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
