Минусование колличетсва товара при его продаже

Рейтинг: -1Ответов: 1Опубликовано: 13.06.2023

нашел статью на зарубежном сайте, практически все что необходимо там есть, + мелкие поправки мои которые будут, но они не внесут большие изменения. Но там я ненашел что бы скрипт отнимал количество товара (типо что лежит в складе) про продажи онного.

Тык что бы глянуть

// Check the session variable for products in cart
$products_in_cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
$products = array();
$subtotal = 0.00;
// If there are products in cart
if ($products_in_cart) {
    // There are products in the cart so we need to select those products from the database
    // Products in cart array to question mark string array, we need the SQL statement to include IN (?,?,?,...etc)
    $array_to_question_marks = implode(',', array_fill(0, count($products_in_cart), '?'));
    $stmt = $pdo->prepare('SELECT * FROM products WHERE id IN (' . $array_to_question_marks . ')');
    // We only need the array keys, not the values, the keys are the id's of the products
    $stmt->execute(array_keys($products_in_cart));
    // Fetch the products from the database and return the result as an Array
    $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
    // Calculate the subtotal
    foreach ($products as $product) {
        $subtotal += (float)$product['price'] * (int)$products_in_cart[$product['id']];
    }
}
?>

а после продажи там банально выдается сообшение что все прошло успешно :

<?=template_header('Place Order')?>

<div class="placeorder content-wrapper">
    <h1>Your Order Has Been Placed</h1>
    <p>Thank you for ordering with us! We'll contact you by email with your order details.</p>
</div>

<?=template_footer()?>

Тык на листинг БД

Подскажите пож-та как реализовать Бью челом об пол ибо сил нахоится за компом нет.

Ответы

▲ 0

вдруг кому будет нужно

<?php
// Подключаем файл конфигурации базы данных
require_once 'config.php';

// Получаем идентификатор товара и количество проданных товаров

$
$product_id = $_POST['product_id'];
$quantity_sold = $_POST['quantity_sold'];

// Обновляем количество доступных товаров в базе данных
$query = "UPDATE products SET quantity = quantity - :quantity_sold WHERE id = :product_id";
$stmt = $db->prepare($query);

$
$stmt->bindParam(':quantity_sold', $quantity_sold);
$stmt->bindParam(':product_id', $product_id);
$stmt->execute();
?>