diff --git a/inc/ajax-fetch.php b/inc/ajax-fetch.php index 3656c9c..d647339 100644 --- a/inc/ajax-fetch.php +++ b/inc/ajax-fetch.php @@ -8,12 +8,11 @@ query("SELECT p.* FROM products as p JOIN categories as c on p.id_cat = c.id and c.lang = '".$_SESSION["l"]."' where p.title LIKE '%".$_POST["fetch"]."%' and p.active = 1 order by p.ord ASC"); while ($row = $db->fetch()) { - // we get the TAX for that product - $rowt["tax"] = 0; // we set default to zero - if (isset($_SESSION["country"])){ // we check if Shibe is logged in and we get only shipping from all countries or his own country - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' and country = '".$_SESSION["country"]."' limit 1")->fetch(); + // we get the TAX for that product (safe if tax table missing) + if (isset($_SESSION["country"])){ + $rowt = $d->GetTaxRow($row["cat_tax"], $_SESSION["country"]); }else{ - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' limit 1")->fetch(); + $rowt = $d->GetTaxRow($row["cat_tax"]); } ?> diff --git a/inc/functions.php b/inc/functions.php index d2791e3..7b31900 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -750,6 +750,27 @@ public function RemoveShipping($id) //// tax ///////// + // Safe tax lookup — returns ['tax' => 0] when table/row is missing (fresh installs). + public function GetTaxRow($category, $country = null) + { + $default = array("tax" => 0); + $category = filter_var((string)$category, FILTER_SANITIZE_ADD_SLASHES); + try { + if ($country !== null && $country !== '') { + $country = filter_var((string)$country, FILTER_SANITIZE_ADD_SLASHES); + $row = $this->pdo->query("SELECT * FROM tax WHERE category = '".$category."' AND country = '".$country."' LIMIT 1")->fetch(); + } else { + $row = $this->pdo->query("SELECT * FROM tax WHERE category = '".$category."' LIMIT 1")->fetch(); + } + if ($row && isset($row["tax"])) { + return $row; + } + } catch (Throwable $e) { + // Missing tax table or column — treat as zero rate so listings stay up. + } + return $default; + } + // Add tax public function InsertTax($category,$country,$tax,$active) { diff --git a/inc/listings.php b/inc/listings.php index d071c6b..31fb7a9 100644 --- a/inc/listings.php +++ b/inc/listings.php @@ -150,34 +150,38 @@ function formatText (icon) { " placeholder="0"> - +
diff --git a/inc/main.php b/inc/main.php index 9c5e52b..bd0f26a 100644 --- a/inc/main.php +++ b/inc/main.php @@ -3,12 +3,11 @@ query("SELECT p.* FROM products as p JOIN categories as c on p.id_cat = c.id and c.lang = '".$_SESSION["l"]."' where p.active = 1 order by p.id DESC"); while ($row = $db->fetch()) { - // we get the TAX for that product - $rowt["tax"] = 0; // we set default to zero - if (isset($_SESSION["country"])){ // we check if Shibe is logged in and we get only shipping from all countries or his own country - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' and country = '".$_SESSION["country"]."' limit 1")->fetch(); + // we get the TAX for that product (safe if tax table missing) + if (isset($_SESSION["country"])){ + $rowt = $d->GetTaxRow($row["cat_tax"], $_SESSION["country"]); }else{ - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' limit 1")->fetch(); + $rowt = $d->GetTaxRow($row["cat_tax"]); } ?> diff --git a/inc/product.php b/inc/product.php index 6e28cb7..7a88585 100644 --- a/inc/product.php +++ b/inc/product.php @@ -82,11 +82,12 @@ function randomDogeemonic(min, max) { }; $rowc = $pdo->query("SELECT * FROM categories where id = '".$row["id_cat"]."' limit 1")->fetch(); $shibe = $pdo->query("SELECT * FROM shibes where id = '".$row["id_shibe"]."' limit 1")->fetch(); - // we get the TAX for that product - $rowt["tax"] = 0; // we set default to zero - if (isset($_SESSION["country"])){ // we check if Shibe is logged in and we get only shipping from all countries or his own country - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' and country = '".$_SESSION["country"]."' limit 1")->fetch(); - }else{ + // we get the TAX for that product (safe if tax table missing) + if (isset($_SESSION["country"])){ + $rowt = $d->GetTaxRow($row["cat_tax"], $_SESSION["country"]); + }else{ + $rowt = $d->GetTaxRow($row["cat_tax"]); + }else{ $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' limit 1")->fetch(); } @@ -441,10 +442,11 @@ function randomDogeemonic(min, max) { query("SELECT p.* FROM products as p JOIN categories as c on p.id_cat = c.id and c.lang = '".$_SESSION["l"]."' where p.id_cat = '".$row["id_cat"]."' and p.id <> '".$_GET["product"]."' and p.active = 1 order by p.ord ASC"); while ($row = $db->fetch()) { - // we get the TAX for that product - $rowt["tax"] = 0; // we set default to zero - if (isset($_SESSION["country"])){ // we check if Shibe is logged in and we get only shipping from all countries or his own country - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' and country = '".$_SESSION["country"]."' limit 1")->fetch(); + // we get the TAX for that product (safe if tax table missing) + if (isset($_SESSION["country"])){ + $rowt = $d->GetTaxRow($row["cat_tax"], $_SESSION["country"]); + }else{ + $rowt = $d->GetTaxRow($row["cat_tax"]); }else{ $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' limit 1")->fetch(); } diff --git a/inc/products.php b/inc/products.php index 44e97bd..2d07fdf 100644 --- a/inc/products.php +++ b/inc/products.php @@ -2,12 +2,11 @@ query("SELECT p.* FROM products as p JOIN categories as c on p.id_cat = c.id and c.lang = '".$_SESSION["l"]."' where p.id_cat = '".$_GET["c"]."' and p.active = 1 order by p.ord ASC"); while ($row = $db->fetch()) { - // we get the TAX for that product - $rowt["tax"] = 0; // we set default to zero - if (isset($_SESSION["country"])){ // we check if Shibe is logged in and we get only shipping from all countries or his own country - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' and country = '".$_SESSION["country"]."' limit 1")->fetch(); + // we get the TAX for that product (safe if tax table missing) + if (isset($_SESSION["country"])){ + $rowt = $d->GetTaxRow($row["cat_tax"], $_SESSION["country"]); }else{ - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' limit 1")->fetch(); + $rowt = $d->GetTaxRow($row["cat_tax"]); } ?> diff --git a/inc/shop.php b/inc/shop.php index d4cba89..c16a97d 100644 --- a/inc/shop.php +++ b/inc/shop.php @@ -45,12 +45,11 @@ query("SELECT p.* FROM products as p JOIN categories as c on p.id_cat = c.id and c.lang = '".$_SESSION["l"]."' where p.id_shibe = '".$_GET["shibe"]."' and p.active = 1 order by p.ord ASC"); while ($row = $db->fetch()) { - // we get the TAX for that product - $rowt["tax"] = 0; // we set default to zero - if (isset($_SESSION["country"])){ // we check if Shibe is logged in and we get only shipping from all countries or his own country - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' and country = '".$_SESSION["country"]."' limit 1")->fetch(); + // we get the TAX for that product (safe if tax table missing) + if (isset($_SESSION["country"])){ + $rowt = $d->GetTaxRow($row["cat_tax"], $_SESSION["country"]); }else{ - $rowt = $pdo->query("SELECT * FROM tax where category = '".$row["cat_tax"]."' limit 1")->fetch(); + $rowt = $d->GetTaxRow($row["cat_tax"]); } ?> diff --git a/shibeship.sql b/shibeship.sql index 85e5479..afaa593 100644 --- a/shibeship.sql +++ b/shibeship.sql @@ -139,4 +139,23 @@ CREATE TABLE `shibes` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --- 2023-07-13 17:16:04 \ No newline at end of file + +DROP TABLE IF EXISTS `tax`; +CREATE TABLE `tax` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `category` varchar(255) NOT NULL DEFAULT '', + `country` varchar(255) DEFAULT NULL, + `tax` decimal(10,4) NOT NULL DEFAULT '0.0000', + `active` int(1) DEFAULT '1', + PRIMARY KEY (`id`), + KEY `idx_tax_category` (`category`), + KEY `idx_tax_country` (`country`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- Default zero-rate row so listings tax dropdown is never empty after fresh install +INSERT INTO `tax` (`id`, `category`, `country`, `tax`, `active`) VALUES +(1,'0',NULL,0.0000,1), +(2,'Standard','',0.0000,1); + + +-- 2023-07-13 17:16:04