<?php

require('../lib/dbclass.php');

$id = $_POST['id'];

if (!$id) {
    $id = 0;
}

$product_name = $_POST['product_name'];
$amount       = $_POST['amount'];




$totalproduct = findQuery("select COUNT(*) nos from tbl_product where product_name='$product_name' and amount=$amount and status=0 and id!=$id");
$count        = $totalproduct[0]['nos'];




if ($count > 0) {
    $message = 'Already Taken.';


    $encodedMessage = urlencode($message);



    $url = "add_product.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";
} else {
    unset($_POST['id']);


    $id = saveArray('tbl_product', $_POST, $id);
}

if ($id) {
    $message = 'Registered successfully!';



    $encodedMessage = urlencode($message);



    $url = "add_product.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";
} else {
    $message = 'Something went wrong, please try again.';


    $encodedMessage = urlencode($message);



    $url = "add_product.php?message={$encodedMessage}";

    // Redirect to the new URL
    echo "<script>window.location.href='{$url}';</script>";
}
