PHP中每个字符串的错误

PHP中每个字符串的错误,php,session,Php,Session,让我先描述一下我在做什么。我正在做一个购物车,允许选择的项目。项目及其各自选择的选项存储在会话中,并显示在购物车中以供客户验证 我有三组代码要显示给您: 首先,创建选项复选框的代码:位于root/config/templates/product-details.php中 <?php foreach($options as $option): ?> <input type="checkbox" name="candies[]" value="<?php echo $o

让我先描述一下我在做什么。我正在做一个购物车,允许选择的项目。项目及其各自选择的选项存储在会话中,并显示在购物车中以供客户验证

我有三组代码要显示给您:

首先,创建选项复选框的代码:位于root/config/templates/product-details.php中

<?php foreach($options as $option): ?>
   <input type="checkbox" name="candies[]" value="<?php echo $option['candy_name']; ?>" /><?php echo $option['candy_name']; ?><br/>
<?php endforeach; ?>
注意这行代码:$\u会话[$product\u id]=$OPTIONSELECTED

最后显示购物车中的选项:位于root/config/templates/shopping-cart.php中

<?php foreach($_SESSION[$product_id] as $options): ?>
    <?php echo $options; ?>
<?php endforeach; ?>
但是如果我将shopping-cart.php代码放在cart.php页面中并注释掉标题;线路一切正常,运行良好。因此,cart.php页面和shopping-cart.php页面之间有问题。但我不知道是什么。任何帮助、建议、想法和解决方案都将非常好,非常感谢!先谢谢你

为了直观地理解这个问题,下面是shopping-cart.php页面的图片。

更新

我认为问题出在root/config/cart/cart.php页面和root/config/templates/shopping-cart.php页面之间。下面是两者的完整代码

root/config/cart/cart.php代码:

<?php

session_start();

require '../config.php';

$product_id = $_GET['id'];

$action = $_GET['action'];

$optionsSelected = array();
if(!empty($_GET['candies'])){
    foreach($_GET['candies'] as $options){
        $optionsSelected[] = $options;
    }
}

function productExists($product_id) {

    require '../config.php';

    $stmt = $db->prepare("SELECT * FROM products WHERE id=$product_id");

    $stmt->execute(array($id, $name));

    $row_count = $stmt->rowCount();
}

if($product_id && $row_count < 0) {

    die("Error. Product Doesn't Exist");
}

switch($action) {

    case "add":
        $_SESSION['cart'][$product_id]++;
        $_SESSION[$product_id] = $optionsSelected;
        session_write_close();
        header('location: ../../cart.html');
    break;

    case "remove":
        $_SESSION['cart'][$product_id]--;
        if($_SESSION['cart'][$product_id] == 0) {
            unset($_SESSION['cart'][$product_id]);
            header('location: ../../cart.html');
        }
        header('location: ../../cart.html');

    break;

    case "empty": 
        unset($_SESSION['cart']);
    break;
}

?>
您是否正在调用每页顶部的session\u start?需要正确利用$\u会话

-编辑-

$_SESSION[$product_id] = $optionsSelected;
header('location: ../../cart.html');
在重定向之前,您的会话实际上可能无法保存,因此您应该尝试以下方法来排除这种情况:

$_SESSION[$product_id] = $optionsSelected;
session_write_close(); // :)
header('location: ../../cart.html');

经过多次尝试和错误,我终于让它完全按照我希望的方式工作

我将root/config/cart/cart.php更改为以下内容:

case "add":
   $_SESSION['cart'][$product_id]++;
   $_SESSION['options '.$product_id] = $optionsSelected;
   header('location: ../../cart.html');
break;
<?php if($_SESSION['options '.$product_id] > 0): ?>
   <?php foreach($_SESSION['options '.$product_id] as $option): ?>
      <p><?php echo $option; ?></p>
   <?php endforeach; ?>
<?php endif; ?>
然后将root/config/templates/shopping-cart.php中的my for eache更改为:

case "add":
   $_SESSION['cart'][$product_id]++;
   $_SESSION['options '.$product_id] = $optionsSelected;
   header('location: ../../cart.html');
break;
<?php if($_SESSION['options '.$product_id] > 0): ?>
   <?php foreach($_SESSION['options '.$product_id] as $option): ?>
      <p><?php echo $option; ?></p>
   <?php endforeach; ?>
<?php endif; ?>

老实说,我真的不知道它是如何工作的,但我成功地消除了所有错误,并按照我的意愿运行了整个系统。干杯

是的,我正在呼叫session\u start;在每一页的顶部。我知道@米切万,好吧!我更新了我的回复,你能试试吗?在这一点上我愿意尝试任何东西!给我一点时间试试。$product_id是从哪里来的?我忘了添加它了。对不起。它来自:$product_id=$\u GET['id'];在root/config/cart/cart.php页面上,您可以看到您将用户重定向到cart.html,并且没有GET for id。在执行foreach之前,您仍然有foreach$\u会话[$product\u id],请尝试执行此var\u dump$\u会话;并查看会话本身中列出的内容。@Jompper-$product\u id自始至终都是声明的,我可以在foreach中的每一页上重复它。