<?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      = addslashes($_POST['description']);
$catid            = $_POST['catid'];
$authorid         = $_POST['authid'];
$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 == 'on') {
    $upcoming = 'yes';
} else {
    $upcoming = 'no';
}

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

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

if ($featured_product == 'on') {
    $featured_product = 'yes';
} else {
    $featured_product = 'no';
}









$stmt = $db->prepare("insert into product values(null,'$title', $price , '$description','',now(),'$display',$catid,$authorid,
'$publishing_date','$edition',$no_of_pages,'$language',$old_price,'$paid_delivery','$featured_product','$upcoming')");

$stmt->execute();



$LAST_ID = $db->lastInsertId();


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

if ($filename) {
    $picname = $LAST_ID;
    $stmt    = $db->prepare("Update product set image='$picname' where id=$LAST_ID");
    $stmt->execute();


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

    move_uploaded_file($filepath, $destfile);
}

header('location:../books.php');
exit();
