1

我确信对此有一个简单的答案,但是我已经摸索了将近一个星期,然后投降了。我正在尝试构建一个购物车应用程序,当我在同一页面上包含代码时,我构建的每个编码解决方案都可以工作,但是当我尝试使用外部页面运行该函数时,它似乎没有返回数据。我尝试了各种监控技术来确定它正在发生什么。

这是主页的代码:

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>Cart Connection</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>
            <p>Will this display?</p>
            <p><a href='<?php echo "showCart.php?PHPSESSID=" . session_id() ?>'>View Cart</a></p>

            <?php
                $Database = "L3TtL2B5DdY";
                $Table = "tblProds";

                if (isset($_SESSION['curCart']))
                    $Cart = unserialize($_SESSION['curCart']);
                else
                {
                    if (class_exists("shoppingCart"))
                    {
                        $Cart = new shoppingCart();
                        $Cart->setDatabase($Database);
                        echo ("<p>If statement ran successfully</p>");
                    }
                    else
                        exit("<p>The shoppingCart class is not available!");
                }


                $Cart->setTable($Table);
                $Cart->getProductList();
                $_SESSION['curCart'] = serialize($Cart);
            ?>
            <p><a href='<?php echo "showCart.php?PHPSESSID=" . session_id() ?>'>View Cart</a></p>

    </body>
</html>

以下是“shoppingCart.php”页面上的相关代码:

<?php
    class shoppingCart
    {
        private $dbConn = "";
        private $dbName = "";
        private $tableName = "";
        private $orders = array();
        private $orderTable = array();

        function _construct()
        {
            $this->dbConn = @new mysqli("localhost", "root", "");
                if (mysqli_connect_errno()) 
                    die("<p>Unable to connect to the database server.</p>" . "<p>Error Code " . 
                    mysqli_connect_errno() . ": " . mysqli_connect_error() . "</p>");
        }

        public function setDatabase($Database)
        {
            $this->dbName = $Database;
            @$this->dbConn->select_db($this->dbName)
                Or die("<p>Unable to select the database.</p>" . "<p>Error code " . mysqli_errno($this->dbConn) . 
                ": " . mysqli_error($this->dbConn) . "</p>");
        }

        public function setTable($Table)
        {
            $this->tableName = $Table;
        }

        public function getProductList()
        {
            $sqlString = "SELECT prodID, prodName, prodPrice FROM $this->tableName";
            @$qryResult = $this->dbConn->query($sqlString)
                Or die("<p>Unable to perform the query.</p>" . "<p>Error code " . mysqli_errno($this->dbConn) . 
                ": " . mysqli_error($this->dbConn) . "</p>");
            echo "<table width='100%' border='1'>";
            echo "<tr><th>Product ID</th><th>Product Name</th><th>Product Price</th><th>Select Item</th></tr>";
            $row = $qryResult->fetch_row();
            do
            {
                echo "<tr><td>{$row[0]}</td>";
                echo "<td>{$row[1]}</td>";
                echo "<td>{$row[2]}</td>";
                echo "<td><a href='showCart.php?PHPSESSID=" . session_id() . "&operation=addItem&productID=" . $row[0] . 

"'>Add</a></td></tr>";
                $row = $qryResult->fetch_row();
            } while ($row);
            echo "</table>";
        }

......
?>

当我尝试加载主页时,它将显示两个

线条,仅此而已。当我第一次创建代码并认为它​​可以工作时,我调试了所有错误。当我编写此页面的原始版本时,我将“连接”代码放在同一页面上,并且表格显示正常,所以我不知道它还能是什么。我在我的 Windows XP 机器上安装了 WAMP,它似乎工作正常。我没有触及任何程序的配置文件,我的所有其他测试代码似乎都可以正常工作。就在我尝试联系外部文件时。任何帮助将不胜感激,因为我认为我的大脑正在变成糊状。谢谢

4

2 回答 2

1

您可能需要在主页中包含 ShoppingCart.php 文件,因此它具有 ShoppingCart 类的定义。尝试在主页顶部放置:

<?php require('ShoppingCart.php'); ?>

我认为可能发生的是购物车对象正在从 Session 中反序列化,但没有类定义,因此它成为不完整类的实例。然后,当您在其上调用方法时,您会遇到致命错误。可能没有帮助的是您可能没有显示错误,因此脚本将结束。您也可以尝试在主页顶部放置:

<?php ini_set('display_errors', true); ?> 

这应该会显示 PHP 错误。

编辑

可能值得指出的是,您不能在会话中存储数据库连接。您需要在每个请求上连接到服务器/选择数据库等。我不认为你的脚本目前正在这样做。

此外,您可以将对象存储在会话中,而不必自己担心序列化,这是一个简单的示例:

<?php
//include class definition before starting session.
require('ShoppingCart.php');

session_start();

if (!isset($session['cart'])) {
    $session['cart'] = new ShoppingCart();
}

$cart = $session['cart'];

//do stuff to cart
$cart->doSomething();

//changes are now saved back to the session when the script is terminated, without you having to do anything.
于 2009-06-11T22:54:28.880 回答
0

你需要

include_once("ShoppingCart.php");

阅读包含文件的不同方法

http://www.w3schools.com/PHP/php_includes.asp

于 2009-06-11T22:53:02.050 回答