shoppingcart中未定义的变量msg

时间:2015-11-25 08:15:50

标签: php

shoppingcart.php中未定义变量msg的问题

我收到此错误提示:未定义的变量:第64行的D:\ wamp \ www \ project \ shoppingcart \ shoppingcart.php中的msg。 msg变量在代码中声明。但为什么它会出错!

Screenshot of error

include("includes/db.php");
include("includes/functions.php");

//if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    remove_product($_REQUEST['pid']);
}
else if(isset($_REQUEST['command'])=='clear'){
    unset($_SESSION['cart']);
}
else if(isset($_REQUEST['command'])=='update'){
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=intval($_REQUEST['product'.$pid]);
        if($q>0 && $q<=999){
            $_SESSION['cart'][$i]['qty']=$q;
        }
        else{
            $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
        }
    }
}

2)这里使用的msg变量

<div style="color:#F00"><?php echo $msg ?></div>

3)购物车变量错误以及未定义

<?php

        if(is_array($_SESSION['cart'])){
            echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
            $max=count($_SESSION['cart']);
            for($i=0;$i<$max;$i++){
                $pid=$_SESSION['cart'][$i]['productid'];
                $q=$_SESSION['cart'][$i]['qty'];
                $pname=get_product_name($pid);
                if($q==0) continue;
        ?>

4 个答案:

答案 0 :(得分:3)

您可以使用<span>

答案 1 :(得分:0)

$msg最初未在任何地方定义。在您包含文件后立即使用$msg = '';来定义变量,以便始终可用。

include("includes/db.php");
include("includes/functions.php");

$msg = ''; // <-- Define here

//if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    remove_product($_REQUEST['pid']);
}
else if(isset($_REQUEST['command'])=='clear'){
    unset($_SESSION['cart']);
}
else if(isset($_REQUEST['command'])=='update'){
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=intval($_REQUEST['product'.$pid]);
        if($q>0 && $q<=999){
            $_SESSION['cart'][$i]['qty']=$q;
        }
        else{
            $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
        }
    }
}

答案 2 :(得分:0)

此通知即将发布,因为$msg未在任何地方定义。使用以下代码。

include("includes/db.php");
include("includes/functions.php");

// Declare $msg here
$msg = '';

//if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    remove_product($_REQUEST['pid']);
}
else if(isset($_REQUEST['command'])=='clear'){
    unset($_SESSION['cart']);
}
else if(isset($_REQUEST['command'])=='update'){
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=intval($_REQUEST['product'.$pid]);
        if($q>0 && $q<=999){
            $_SESSION['cart'][$i]['qty']=$q;
        }
        else{
            $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
        }
    }
}

答案 3 :(得分:0)

看起来你正试图使用​​$ msg是否存在,这就是错误的来源。试试这个:    <div style="color:#F00"><?php if(isset($msg)){ echo $msg; } ?></div>