Php 使用PayPal智能按钮购买后未更新股票价值
我有一个付款的每个产品的付款页面。我已经这样做了,当一个完成的付款,股票价值的变化。我通过ajax使用fetchapi来实现这一点。然而,代码似乎不起作用 这是我目前的代码。 查看_product.php:Php 使用PayPal智能按钮购买后未更新股票价值,php,mysql,paypal,payment-gateway,Php,Mysql,Paypal,Payment Gateway,我有一个付款的每个产品的付款页面。我已经这样做了,当一个完成的付款,股票价值的变化。我通过ajax使用fetchapi来实现这一点。然而,代码似乎不起作用 这是我目前的代码。 查看_product.php: </div> <?php include 'pdo_connect.php'; $product_id = $_GET['product']; $stmt = $db->prepare("SELECT * FROM products Where
</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>";
}
}
?>
正如评论中提到的,update_stock.php文件完全没有任何作用——其中没有任何语句,只有一个变量名
具体来说,如果要对数据库中的现有行进行更改,则需要使用PHP语句运行带有“UPDATE”语句的SQL查询。您需要添加一些代码,在购买后更新数据库中的相应值。
<?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
]);
}
?>
<?php
$_POST['amountOrdered'];
?>