0

我正在制作一个上面有产品的网站。我遇到了意外的文件结束错误。这是整个代码。我还有一个运行这两个功能的页面,同时需要这个我称之为“购物车”的页面。

<?php
include('inc/connect.php');
session_start();
?>
<html>
    <head>
        <title>Cart</title>
    <link rel='stylesheet' href='css/main.css' />
</head>
<body>
    <?php
    $page = 'index.php';

    if(isset($_GET['add'])){
        $add_id = (int)$_GET['add'];
        $quantity = mysql_query("SELECT id, quantity FROM products WHERE id='$add_id'");
        while($quantity_row = mysql_fetch_assoc($quantity)){
            if($quantity_row['quantity'] !=@$_SESSION['cart_'.(int)$add_id]){
                @$_SESSION['cart_'.(int)$_GET['add']]+='1';
            }
        }

    }

    function products(){
        $get = mysql_query("SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC");
        if(mysql_num_rows($get) == 0){
            echo "There are no products to display.";
        }
        else{
            while($get_row = mysql_fetch_assoc($get)){
                echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />'.$get_row['price'].' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
            }
        }
    }

    function cart(){
        foreach($_SESSION as $name => $value){
            if($value>0){
                if(substr($name, 0, 5) == 'cart_'){
                    $id = mysql_real_escape_string(substr($name, 5, (strlen($name)-5)));
                    $get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
                    while($get_row = mysql_fetch_assoc($get)){
                        $sub = $get_row['price']*$value;
                        echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
                }
            }
            else{
                echo "Your cart is empty";
            }
        }
    }

    ?>
    </body>
</html>
4

2 回答 2

2

您的最后一个功能永远不会关闭。如果您正确缩进(PHP 是如何读取它的):

function cart(){
    foreach($_SESSION as $name => $value){
        if($value>0){
            if(substr($name, 0, 5) == 'cart_'){
                $id = mysql_real_escape_string(substr($name, 5, (strlen($name)-5)));
                $get = mysql_query("SELECT id, name, price FROM products WHERE id='$id'");
                while($get_row = mysql_fetch_assoc($get)){
                    $sub = $get_row['price']*$value;
                    echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
                }
            }
            else{
                echo "Your cart is empty";
            }
        }
    }
//????

看起来你忘了结束你的while陈述。

于 2013-02-15T21:51:49.990 回答
2

您缺少此 while 循环的右括号:

while($get_row = mysql_fetch_assoc($get)){
    $sub = $get_row['price']*$value;
    echo $get_row['name'].' x '.$value.' @ $'.number_format($get_row['price'], 2).' = $'.number_format($sub, 2);
于 2013-02-15T21:51:50.450 回答