<?php

ob_start();

if (!session_start()) {
    session_start();
}

try {
    $db = new PDO('mysql:host=localhost;dbname=gdhhsxmy_monsoonvibespublica;charset=utf8', 'gdhhsxmy_monsoonvibespublica', 'monsoonvibespublica');
} catch (Exception $e) {
    echo 'Error has Occured';
}



$title            = $_POST['title'];
$price            = $_POST['price'];
$description      = $_POST['description'];
$catid            = $_POST['catid'];
$authorid         = $_POST['authorid'];
$publishing_date  = $_POST['publishing_date'];
$edition          = $_POST['edition'];
$no_of_pages      = $_POST['no_of_pages'];
$language         = $_POST['language'];
$old_price        = $_POST['old_price'];
$paid_delivery    = $_POST['paid_delivery'];
$display          = $_POST['Display'];
$featured_product = $_POST['featured_product'];
$upcoming         = $_POST['upcoming'];

if ($upcoming == 'Yes') {
    $upcoming = 'yes';
} else {
    $upcoming = 'no';
}

if ($paid_delivery == 'Yes') {
    $paid_delivery = 'yes';
} else {
    $paid_delivery = 'no';
}

if ($display == 'Yes') {
    $display = 'yes';
} else {
    $display = 'no';
}

if ($featured_product == 'Yes') {
    $featured_product = 'yes';
    $stmt             = $db->prepare("UPDATE product SET featured_product='no'");
    $stmt->execute();
} else {
    $featured_product = 'no';
}

$n1 = trim($_POST['id']);


$file      = $_FILES['photo'];
$filename  = $file['name'];
$filepath  = $file['tmp_name'];
$fileerror = $file['error'];

if ($filename) {
    $path = '../../pic/book/' . $n1;
    unlink($path);
    $picname = $n1;
    $stmt    = $db->prepare("Update product set image='$picname' where id=$n1");
    $stmt->execute();


    $destfile = '../../pic/book/' . $picname;
    //ECHO  $destfile;

    move_uploaded_file($filepath, $destfile);
}





$stmt = $db->prepare("UPDATE product SET title='$title' , price=$price, description='$description',display='$display',catid=$catid,authorid=$authorid,publishing_date='$publishing_date',
 edition='$edition' ,no_of_pages=$no_of_pages,language='$language',old_price=$old_price,paid_delivery='$paid_delivery',featured_product='$featured_product',upcoming='$upcoming' where id=$n1 ");
$stmt->execute();
header('location:../books.php');
exit();
