使用 PayPal 智能按钮购买后股票价值未更新

Stock value not updating after purchase with PayPal smart button

我有一个用于付款的每个产品的结帐页面。我已经这样做了,当一个人完成付款时,股票价值就会发生变化。我通过 ajax 使用 fetch-api 来完成此操作。但是,代码似乎不起作用。

这是我当前的代码。 view_product.php:

    </div>

<?php
include 'pdo_connect.php';

$product_id = $_GET['product'];

$stmt = $db->prepare("SELECT * FROM products Where id = :id");
$stmt->bindParam(':id', $product_id );
$stmt->execute();

    while($row = $stmt->fetch()){
        $stock = $row['stock'];
        
        if ($stock > 0){
        echo 
        "<script>
        paypal.Buttons({

            style: {
                shape: 'rect',
                color: 'blue',
                layout: 'vertical',
                label: 'paypal',
                locale: 'en_CY'
            },
        
            createOrder: async function(data, actions) {
                let stock = (await fetch('get_current_stock.php')).json();
                let currentStock = stock['current'];
        
                // you may want to check for amoutTryingToOrder instead of 1
                if (currentStock < 1) {
                    alert('Out of stock, sorry :(');
                    return false;
                }
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: ". $row['paypal_price'] .",
                            currency: 'EUR'
                        }
                    }]
                });
            },
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    //alert('Transaction completed by ' + details.payer.name.given_name + '!');
                    alert('Your payment has been processed!');
                    localStorage.clear();
                    window.location.href = 'http://localhost/YourLocalArtist/thankyou.php';
                    
        
                    const formData = new FormData();
                    formData.append('amountOrdred', 1);
        
                    fetch('update_stock.php', {
                        method: 'POST',
                        body: formData
                    });
                })
            }
        }).render('#paypal-button-container');
      </script>";
    }
    else{
    echo   
    "<h1>OUT OF STOCK</h1>
    <script>render('#paypal-button-container');
    </script>";
    }
   }
 ?>

get_current_stock.php:

<?php
  header('Content-Type: application/json');

  include 'pdo_connect.php';

  $connect = mysqli_connect('localhost', 'root','', 'yourlocalartist-db');
  $query = 'SELECT * FROM products ORDER by id ASC';
  $result = mysqli_query($connect, $query);

  while($product = mysqli_fetch_assoc($result)){
    $currentStock = $product['stock'];

  echo json_encode([
   'current' => $currentStock
   ]);
  }
?>

update_stock.php:

<?php

 $_POST['amountOrdered'];

?>

我做错了什么?提前致谢!

正如评论中提到的,您的 update_stock.php 文件什么都不做——里面没有语句,只有一个变量名。

具体来说,如果您想更改数据库中的现有行,您将需要 PHP 语句 运行 SQL 查询和 'UPDATE' 语句