未定义的变量:会话开始(购物车尝试)

时间:2015-04-17 12:33:58

标签: php session

我没有专家,只是一名学生,我正在尝试创建一个购物车,到目前为止我明白我必须使用会话来存储ID。我连接到我的数据库的方式总是这样:

<html>

<head>Shopping cart</head>

<?php

$session_start();

?>

<body>
<?php
    $serverName = "ephesus.cs.cf.ac.uk";
    $dbName = "c1429814";
    $user = "c1429814";
    $pass = "fakepassword";

    $con = mysqli_connect($serverName, $user, $pass, $dbName);

    if(!$con){
        die("failure to connect to the server ".mysqli_connect_error());
    }


    echo "<h1> Shopping cart </h1><br/>";   

    echo "<div class='text_border'>";

    if(isset($_GET['id']))
        $id=$_GET['id'];
    else
        $id=1;

    if(isset($_GET['action']))
        $action=$_GET['action'];
    else
        $action="empty";

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

    break;
    case "remove":
        if (isset($_SESSION['cart'][$id]))
        {
            $_SESSION['cart'][$id]--;
            if($_SESSION['cart'][$id]==0)
                unset($_SESSION['cast'][$id]);

        }   

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

    /*Display cart */
    if (isset($_SESSION['cart']))
    {
        echo "<table border = 0 cellspacing=0 width='500'>";
        $total = 0;
    foreach($_SESSION['cart'] as $id => $x)
    {   
        $query = "SELECT * FROM Software WHERE id = '$product' ";
        $result = mysqli_query($con, $query);


        $row = mysqli_fetch_array($result);
        $name = $row['name'];
        $name=substr($name, 0, 40);
        $price = $row['price'];
        $the_cost= $price * $x;
        $total = $total+$the_cost;

        echo "<tr>";
        echo "<td align='left'>$name </td>";
        echo "<td align='right'>$x <a           href='cart.php>id.".$id.">action=remove'>reduce'</a></td>";
        echo "<td align='right'>= $the_cost";
        echo "</tr>";
    }
        echo "<tr>";
        echo "<td align='right'><br>Total=</td>";
        echo "<td align='right'><b><br> $total </b></td>";
        echo "</tr>";
        echo "</table>";
    }
    else
        echo "Cart is empty";
    }

    ?>


</body>


</html>

我收到一个错误,称为未定义的变量会话,我不确定我需要定义哪种变量来解决这个问题?或者是我连接数据库的方式?任何可以给我足够帮助和提示的信息将不胜感激。 :)

4 个答案:

答案 0 :(得分:1)

尝试在HTML之前打开会话并删除$

像这样:

<?php
  session_start();
?>

<html>

<head>Shopping cart</head>


<body>
<?php
    $serverName = "ephesus.cs.cf.ac.uk";
    $dbName = "c1429814";
    $user = "c1429814";
    $pass = "ugsok4";

    $con = mysqli_connect($serverName, $user, $pass, $dbName);

    if(!$con){
        die("failure to connect to the server ".mysqli_connect_error());
    }


    echo "<h1> Shopping cart </h1><br/>";   

    echo "<div class='text_border'>";

    if(isset($_GET['id']))
        $id=$_GET['id'];
    else
        $id=1;

    if(isset($_GET['action']))
        $action=$_GET['action'];
    else
        $action="empty";

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

    break;
    case "remove":
        if (isset($_SESSION['cart'][$id]))
        {
            $_SESSION['cart'][$id]--;
            if($_SESSION['cart'][$id]==0)
                unset($_SESSION['cast'][$id]);

        }   

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

    /*Display cart */
    if (isset($_SESSION['cart']))
    {
        echo "<table border = 0 cellspacing=0 width='500'>";
        $total = 0;
    foreach($_SESSION['cart'] as $id => $x)
    {   
        $query = "SELECT * FROM Software WHERE id = '$product' ";
        $result = mysqli_query($con, $query);


        $row = mysqli_fetch_array($result);
        $name = $row['name'];
        $name=substr($name, 0, 40);
        $price = $row['price'];
        $the_cost= $price * $x;
        $total = $total+$the_cost;

        echo "<tr>";
        echo "<td align='left'>$name </td>";
        echo "<td align='right'>$x <a           href='cart.php>id.".$id.">action=remove'>reduce'</a></td>";
        echo "<td align='right'>= $the_cost";
        echo "</tr>";
    }
        echo "<tr>";
        echo "<td align='right'><br>Total=</td>";
        echo "<td align='right'><b><br> $total </b></td>";
        echo "</tr>";
        echo "</table>";
    }
    else
        echo "Cart is empty";
    }

    ?>


</body>


</html>

答案 1 :(得分:1)

$用于php变量... 你使用函数时不需要$, 并在所有代码上方打开它......

所以这里:

//...
session_start();
//...

会更好......

答案 2 :(得分:1)

使用session_start();(不带美元符号),因为您正在调用不访问变量的函数。并且作为旁注:&#34;要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。&#34;更多信息:http://php.net/manual/en/function.session-start.php

答案 3 :(得分:1)

请使用以下代码简化您的代码。 &#39; $&#39;不应该在你的session_start();

中使用
<?php
session_start();
?>

<head>Shopping cart</head>


<body>
    <?php
    $serverName = "ephesus.cs.cf.ac.uk";
    $dbName = "c1429814";
    $user = "c1429814";
    $pass = "ugsok4";

    $con = mysqli_connect($serverName, $user, $pass, $dbName);

    if (!$con) {
        die("failure to connect to the server " . mysqli_connect_error());
    }


    echo "<h1> Shopping cart </h1><br/>";

    echo "<div class='text_border'>";

    $id = isset($_GET['id']) ? $_GET['id'] : 1;
    $action = isset($_GET['action']) ? $_GET['action'] : 'empty';

    switch ($action) {
        case "add":
            if (isset($_SESSION['cart'][$id])) {
                $_SESSION['cart'][$id] ++;
            } else {
                $_SESSION['cart'][$id] = 1;
            }
            break;

        case "remove":
            if (isset($_SESSION['cart'][$id])) {
                $_SESSION['cart'][$id] --;

                if ($_SESSION['cart'][$id] == 0)
                    unset($_SESSION['cast'][$id]);
            }

            break;

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

            /* Display cart */
            if (isset($_SESSION['cart'])) {
                echo "<table border = 0 cellspacing=0 width='500'>";
                $total = 0;
                foreach ($_SESSION['cart'] as $id => $x) {
                    $query = "SELECT * FROM Software WHERE id = '$product' ";
                    $result = mysqli_query($con, $query);
                    $row = mysqli_fetch_array($result);
                    $name = $row['name'];
                    $name = substr($name, 0, 40);
                    $price = $row['price'];
                    $the_cost = $price * $x;
                    $total = $total + $the_cost;

                    echo "<tr>";
                    echo "<td align='left'>$name </td>";
                    echo "<td align='right'>$x <a           href='cart.php>id." . $id . ">action=remove'>reduce'</a></td>";
                    echo "<td align='right'>= $the_cost";
                    echo "</tr>";
                }
                echo "<tr>";
                echo "<td align='right'><br>Total=</td>";
                echo "<td align='right'><b><br> $total </b></td>";
                echo "</tr>";
                echo "</table>";
            } else {
                echo "Cart is empty";
            }
    }
    ?>


</body>