【问题标题】:PHP refresh page re-initialises variable to 0PHP刷新页面将变量重新初始化为0
【发布时间】:2012-11-09 07:26:58
【问题描述】:

我正在使用 URL ?action=add 将商品添加到购物车。我还没有初始化变量$count 并且收到以下错误:

Notice: Undefined index: count in C:\wamp\www\cart.php on line 31

Notice: Undefined index: count in C:\wamp\www\cart.php on line 32

我知道我需要定义 $count 变量。 ($count = 0;) 但我不希望每次用户将新商品添加到购物车时刷新页面并将 SESSION 变量重置为 0。 $count 需要用作下一页的计数器。我该怎么做?

这是我的案例代码:

switch($action) { 
    case "add":
        if (isset($_SESSION['cart'][$comic_id])) {
            $count=$_SESSION['count']; //LINE 31
            $_SESSION['count']++; //LINE 32
            $_SESSION['cart'][$comic_id]++;

        } else {
            $_SESSION['cart'][$comic_id] = 1;
            }
    break;  

添加后 if (!isset($_SESSION['count'])) $_SESSION['count'] = 1;

<?php 
if(!isset($_SESSION)) {
     session_start();
}
$total = 0;
$page = 'cart';

$dbname = "crystal_fusion";
$dbhost = "127.0.0.1";
$dbuser = "root";
$dbpwd = "";

$link = mysql_connect($dbhost, $dbuser, $dbpwd);

mysql_select_db($dbname);

if(isset($_GET['comic_id'])){
    $comic_id = $_GET['comic_id'];
} else {
    $comic_id = NULL;}

$action = $_GET['action'];

if($comic_id && !productExists($comic_id)) {
    die("We apologise, but we can't seem to find the comic you were looking for!!!");
}

if (!isset($_SESSION['count'])) $_SESSION['count'] = 1; //added as suggested - now output is correct when adding multiples of 1 item. However, the count is wrong if more than 1 item is added.

switch($action) {
    case "add":
        if (isset($_SESSION['cart'][$comic_id])) {
            $count=$_SESSION['count'];
            $_SESSION['count']++;
            $_SESSION['cart'][$comic_id]++;

        } else {
            $_SESSION['cart'][$comic_id] = 1;
            }
    break;  
    case "remove":
        if (isset($_SESSION['cart'][$comic_id])) {
            $_SESSION['cart'][$comic_id]--;} 
        if($_SESSION['cart'][$comic_id] == 0) unset($_SESSION['cart'][$comic_id]); 
    break;  
    case "empty":
        unset($_SESSION['cart']); 
        session_destroy();
    break;  
}

require('header.php');
require('sidebar.php');
if (isset($_SESSION['cart'][$comic_id])){

    echo "<table border=\"0\" padding=\"10\" width=\"80%\">";
    echo "<td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";
    echo "<td colspan=\"6\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Crystal Fusion: Are you sure you wish to empty your cart?');\">Empty Cart</a></td>"; //empty cart link --Sam                      
    echo "<tr height=\"20px\">";
    echo "<tr height=\"20px\">";
    echo "<td align=center>Image</td><td align=center>Title</td><td align=center>Description</td><td colspan=3 align=center>Copies (+/-)</td><td align=center>Price</td>";
    echo "<tr height=\"20px\">";


    foreach($_SESSION['cart'] as $comic_id => $qty) {   

        $sql = sprintf("SELECT title, description, cost, image_thumbnail
                FROM comic 
                WHERE comic_id = %d;",$comic_id);

        $result = mysql_query($sql);

        if(mysql_num_rows($result) > 0) {

            list($name, $description, $price, $image_thumbnail) = mysql_fetch_row($result);



            $cost = $price * $qty;  
            $total = $total + $cost; 

            $cost = number_format($cost,2);
            $total = number_format($total,2);
            $description =  substr($description, 0, 250);

            echo "<br><tr>";
            echo "<td width=\"10px\" align=\"center\"><img height=100 align=center src=\"$image_thumbnail\">";
            echo "<td align=\"center\">$name</td>";
            echo "<td width=\"40%\" align=\"center\">$description...<a href=comic_dyn.php?comic_id=$comic_id>More Info</td>";
            echo "<td width=\"30px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=add&comic_id=$comic_id\">+<br></a><td align=\"center\">$qty <td width=\"20px\" align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&comic_id=$comic_id\">-</a></td>";
            echo "<td align=\"right\">$$cost</td>";
            echo "</tr>";           
        }

    }

    echo "<br><tr><tr height=100px>";
    echo "<td><td><td colspan=\"4\" align=\"right\">Total:</td>";
    echo "<td width=\"60px\" align=\"right\">$$total</td>";
    echo "<tr><td colspan=\"7\" align=\"right\"><a href=\"checkout_html.php\">Proceed to Checkout</a>";
    echo "<tr height=\"50px\">";
    echo "</table>";
}else{

    echo "Your cart is currently empty.";
    echo "<br><br><td colspan=\"1\" align=\"left\"><a href=\"title.php\">Continue Shopping</a></div>";

}

$_SESSION['totnamqty'][]=$name . " " . $qty . " " . $price;

function productExists($comic_id) { 
        $sql = sprintf("SELECT * FROM comic WHERE comic_id = %d;", $comic_id);
    return mysql_num_rows(mysql_query($sql)) > 0;
    }
?>

【问题讨论】:

    标签: php


    【解决方案1】:

    检查这些:

    1. 您必须首先使用session_start(); 初始化会话
    2. 确保$_SESSION['count'] 存在并初始化,然后再分配或对其执行 som 操作。

    【讨论】:

    • 是 session_start 已初始化。如果我设置 $_SESSION['count'] = 0;然后每次页面刷新时计数将设置为 0。我不希望这种情况发生。
    • $count$_SESSION 变量的初始化?
    • 有什么办法解决这个问题吗?
    • 一种方法是显示完整的 PHP 代码,如果您没有任何问题。 :) 这样我们就可以看到你哪里出错了。 :)
    【解决方案2】:

    在使用$_SESSION['count'] 之前,您必须对其进行初始化。我认为你没有这样做,这就是你收到通知的原因。

    【讨论】:

    • 是的,但是如果我将它初始化为0,那么每次刷新页面时计数都会重置为0。
    • @Sam:所以,只有在尚未设置时才设置它。 if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
    • 如果用户处于登录状态,则初始化 $_SESSION['count']=1
    • 如果我的答案有用,请点击我的答案作为正确答案,并请投票给我的答案..谢谢老兄..
    • 检查计数增加的原因......或者如果您不想增加计数并使其保持不变,请将其定义为常量
    【解决方案3】:

    有关更多会话信息,请参阅以下链接:

    W3Schools - PHP Sessions

    (我会发布 PHP.net 手册页,但我是新手,只能发布 2 个链接)

    此外,您可能还想转换为使用 $_POST 变量,而不是通过 URL 传递键值对:

    PHP.net - $_POST

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      相关资源
      最近更新 更多