【发布时间】:2019-09-19 15:37:44
【问题描述】:
如何将购物车中的数据发送到 mysql 数据库,目前我可以将商品添加到购物车中并且可以清除但是我也创建了结帐按钮但无法弄清楚如何从购物车中获取数据到mysql 数据库,我已经尝试过 check_out 操作,但我这样做完全错了
这是我的 action.php,它将购物车中的商品放入数组中并添加或删除它们。
<?php
include('cart/database_connection.php');
//action.php
session_start();
if(isset($_POST["action"]))
{
if($_POST["action"] == "add")
{
if(isset($_SESSION["shopping_cart"]))
{
$is_available = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
{
$is_available++;
$_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
}
}
if($is_available == 0)
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
else
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
if($_POST["action"] == 'remove')
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["product_id"] == $_POST["product_id"])
{
unset($_SESSION["shopping_cart"][$keys]);
}
}
}
if($_POST["action"] == 'empty')
{
unset($_SESSION["shopping_cart"]);
}
if($_POST["action"] == 'check_out')
{
if(isset($_SESSION["shopping_cart"]))
{
foreach($_SESSION["shopping_cart"] as $values)
{
$sql ="INSERT INTO orders (total, product_id)
values ('{$values['product_id']}','{$v['total']}')";
$statement = $connect->prepare($query);
$statement->execute();
if ($statement) {
$_SESSION['success'] = 'Information updated successfully';
header("location: my_account.php");
exit;
} else {
$_SESSION['errormsg'] = 'Someting is wrong in updating your Information, Please try again later.';
header("location: my_account2.php");
exit;
}}
}
}
?>
这是我正在使用调用 index.php 中的函数的脚本
<script>
$(document).ready(function(){
load_cart_data();
function load_product()
{
$.ajax({
url:"cart/fetch_item.php",
method:"POST",
success:function(data)
{
$('#display_item').html(data);
}
});
}
function load_cart_data()
{
$.ajax({
url:"cart/fetch_cart.php",
method:"POST",
dataType:"json",
success:function(data)
{
$('#cart_details').html(data.cart_details);
$('.total_price').text(data.total_price);
$('.badge').text(data.total_item);
}
});
}
$('#cart-popover').popover({
html : true,
container: 'body',
content:function(){
return $('#popover_content_wrapper').html();
}
});
$(document).on('click', '.add_to_cart', function(){
var product_id = $(this).attr("id");
var product_name = $('#name'+product_id+'').val();
var product_price = $('#price'+product_id+'').val();
var product_quantity = $('#quantity'+product_id).val();
var action = "add";
if(product_quantity > 0)
{
$.ajax({
url:"cart/action.php",
method:"POST",
data:{product_id:product_id, product_name:product_name, product_price:product_price, product_quantity:product_quantity, action:action},
success:function(data)
{
load_cart_data();
alert("Item has been Added into Cart");
}
});
}
else
{
alert("Please Enter Number of Quantity");
}
});
$(document).on('click', '.delete', function(){
var product_id = $(this).attr("id");
var action = 'remove';
if(confirm("Are you sure you want to remove this product?"))
{
$.ajax({
url:"cart/action.php",
method:"POST",
data:{product_id:product_id, action:action},
success:function()
{
load_cart_data();
$('#cart-popover').popover('hide');
alert("Item has been removed from Cart");
}
})
}
else
{
return false;
}
});
$(document).on('click', '#clear_cart', function(){
var action = 'empty';
$.ajax({
url:"cart/action.php",
method:"POST",
data:{action:action},
success:function()
{
load_cart_data();
$('#cart-popover').popover('hide');
alert("Your Cart has been clear");
}
});
});
$(document).on('click', '#check_out_cart', function(){
var action = 'empty';
$.ajax({
url:"cart/action.php",
method:"POST",
data:{action:action},
success:function()
{
load_cart_data();
$('#cart-popover').popover('hide');
alert("Your Cart has been clear");
}
});
});
});
</script>
数据库连接.php
<?php
//database_connection.php
$connect = new PDO("mysql:host=localhost;dbname=foodsystem", "root", "");
?>
我已经检查了购物车会话,我可以看到数组中的项目
Array
(
[0] => Array
(
[product_id] => 1
[product_name] => Chicken Burger
[product_price] => 10
[product_quantity] => 1
)
[1] => Array
(
[product_id] => 2
[product_name] => Fish Burger
[product_price] => 10
[product_quantity] => 1
)
[2] => Array
(
[product_id] => 3
[product_name] => Ham Burger
[product_price] => 10
[product_quantity] => 1
)
【问题讨论】:
-
警告:使用 PDO 时,您应该使用带有占位符值的 prepared statements,并将任何用户数据作为单独的参数提供。在此代码中,您可能有严重的SQL injection bugs。永远不要使用字符串插值或连接,而是使用 prepared statements 并且永远不要将
$_POST、$_GET或任何用户数据直接放在查询中。有关一般指导和建议,请参阅 PHP The Right Way。 -
除非这是一个学术项目,否则请记住,编写电子商务平台是一项非常耗时的工作,而且当有许多 existing platforms 可以根据您的需要进行调整和扩展时,这不是必要的练习,或者可以大大简化您的构建并帮助它符合某种组织代码标准的框架。
-
虽然有很多免费的软件适配,但是在更新任何东西之前你必须先了解它是如何制作的。我更喜欢创建自己的,无论何时。
-
@tadman 这是学术项目,只要它有点简单我没问题,但只是停留在这部分
-
值得切换到准备好的语句模型,这样您就不会因语法错误而绊倒。它还使您的查询更容易让我们阅读和帮助您。您确定
$_SESSION设置正确吗?