简体   繁体   English

从 SQL 数据库到购物车数组的数据

[英]Data from a SQL database to an array for a shopping cart

I'm trying to pass data I've retrieved from my SQL Database to a $_SESSION array for a shopping cart to be displayed to the user.我正在尝试将从 SQL 数据库中检索到的数据传递到$_SESSION数组,以便向用户显示购物车。 I don't think the information is actually getting passed over, the program ends up displaying but it gives me blank fields.我认为信息实际上并没有被传递,程序最终显示但它给了我空白字段。 For reference, I'm trying to pass over data through multiple PHP files.作为参考,我正在尝试通过多个 PHP 文件传递数据。 Additionally, once I pass the information over, I'm unsure how I would loop said $_SESSION array the user to add multiple items.此外,一旦我传递了信息,我不确定我将如何循环所说的 $_SESSION 数组用户添加多个项目。

I'm not sure if the issue is through my include() with my index , or maybe my form action in the product_list .我不确定问题是通过我的indexinclude()还是我在product_list中的form action But somewhere either the data isn't passing or it doesn't work the way I think it does.但是在某个地方,要么数据没有通过,要么它不像我认为的那样工作。

This is the code within my product_list where the data is initially sent这是我的product_list中最初发送数据的代码

foreach ($products as $product)  : ?>
                <tr>

                    <td><?php echo $product['productID']; ?></td>
                    <td><?php echo $product['productName']; ?></td>
                    <td class="right"><?php echo $product['price']; ?></td>
                    <td><form action="../Cart/index.php" method="post">
                        <input type="hidden" name="action"
                               value="add_product">
                        <input type="hidden" name="product_name"
                               value="<?php echo $product['productName'];?>"> 
                        <input type="hidden" name="product_price"
                               value ="<?php echo $product['price'];?>">
                        <input type="submit" value="Add to Cart">
                    <?php endforeach; 

This is the code within my index where the the shopping cart is included这是我的index中包含购物车的代码

if ($action == 'add_product') 
{   

    include('../Cart/index.php');
}

This is my code within the ```index within the shopping cart path这是我在购物车路径中的```索引中的代码

$action = 'add_to_cart';


if(!isset($_SESSION['cart']))
{
    $_SESSION['cart'] = array();
}

if($action == 'add_to_cart')
{
    $cart_product_name = filter_input(INPUT_GET, 'product_name');
    $cart_product_price = filter_input(INPUT_GET, 'product_price', FILTER_VALIDATE_FLOAT);
    $product = array('price' => $cart_product_price, 'name' => $cart_product_name);
    $_SESSION['cart'][] = $product; 
   include('cart_view.php');
}

And this is where the $_SESSION array should be displaying这就是$_SESSION数组应该显示的地方

<main>
        <h1>Your Cart</h1>
        <link rel='stylesheet' type="text/css" href="main.css">
        <?php
            print_r($_SESSION['cart'][0]);
        ?>  

What's actually being displayed is this Array ( [price] => [name] => )实际显示的是这个Array ( [price] => [name] => )

The first problem I see is that your input names are incorrect.我看到的第一个问题是您的输入名称不正确。 Every product name input is currently name="product_name", likewise for product_price.每个产品名称输入当前都是 name="product_name",product_price 也是如此。 You need to pass these through as an array, or each with their own unique key.您需要将这些作为数组传递,或者每个都有自己的唯一键。 Otherwise you get one single post value for product_name and product_price - which will be whatever the last inputs with those names are in the form.否则,您将获得 product_name 和 product_price 的一个帖子值 - 这将是表单中带有这些名称的最后输入的任何内容。

You can pass them as an array by doing name="product_name[]" and then your post will contain an array of all those values, in the order that they appear in the form.您可以通过 name="product_name[]" 将它们作为数组传递,然后您的帖子将包含所有这些值的数组,按照它们在表单中出现的顺序。

Next, you say your output is currently Array ( [price] => [name] => ).接下来,您说您的 output 当前是 Array ( [price] => [name] => )。 This indicates to me that your session is in fact working, because you have keys in that array.这向我表明您的 session 实际上正在工作,因为您在该数组中有键。 So actually setting the session is not the problem.所以实际上设置 session 不是问题。 So then you go back to where the session is being set, and see that you are using INPUT_GET in your filter_input parameters - but you are receiving POST values.那么你 go 回到设置 session 的位置,并看到你在 filter_input 参数中使用 INPUT_GET - 但你正在接收 POST 值。 Your parameter should not be INPUT_GET, it should be INPUT_POST.您的参数不应该是 INPUT_GET,而应该是 INPUT_POST。

Once you get all that cleared up you're still going to have a problem, because the inputs for product_name and product_price are going to be arrays with multiple values.一旦你把所有的东西都弄清楚了,你仍然会遇到问题,因为 product_name 和 product_price 的输入将是 arrays 具有多个值。 You need to loop through them to assemble the final array that gets set to the session.您需要遍历它们以组装设置为 session 的最终数组。 I'll leave that to you to explore, because that will look far different than what you currently have going on in your add_to_cart action.我将把它留给你去探索,因为这看起来与你当前在 add_to_cart 操作中所做的完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM