-2

这些是错误

注意:未定义变量:第 7 行 C:\xampp\htdocs\PHPExercise\go4shop\functions.php 中的错误

函数.php

<?php
function pf_validate_number($value, $function, $redirect) {
    if(isset($value) == TRUE) {
        if(is_numeric($value) == FALSE) {
            $error = 1;
        }
        if($error == 1) {
            header("Location: " . $redirect);
        }
        else {
            $final = $value;
        }
    }
    else {
        if($function == 'redirect') {
            header("Location: " . $redirect);   
        }
        if($function == "value") {
            $final = 0;
        }
    }
    return $final;
}


function showcart() {
    if(isset($_SESSION['SESS_ORDERNUM']))   {
        if(isset($_SESSION['SESS_LOGGEDIN']))   {
            $custsql = "SELECT id, status from orders WHERE customer_id = ". $_SESSION['SESS_USERID']. " AND status < 2;";
            $custres = mysql_query($custsql);
            $custrow = mysql_fetch_assoc($custres);
            $itemssql = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id =products.id AND order_id = " . $custrow['id'];
            $itemsres = mysql_query($itemssql);
            $itemnumrows = mysql_num_rows($itemsres);
        }
        else {
            $custsql = "SELECT id, status from orders WHERE session = '" . session_id(). "' AND status < 2;";
            $custres = mysql_query($custsql);
            $custrow = mysql_fetch_assoc($custres);
            $itemssql = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id = products.id AND order_id = " . $custrow['id'];
            $itemsres = mysql_query($itemssql);
            $itemnumrows = mysql_num_rows($itemsres);
        }
    }
    else    {
        $itemnumrows = 0;
    }
    if($itemnumrows == 0) {
        echo "You have not added anything to your shopping cart yet.";
    }
    else {
        echo "<table cellpadding='10'>";
        echo "<tr>";
        echo "<td></td>";
        echo "<td><strong>Item</strong></td>";
        echo "<td><strong>Quantity</strong></td>";
        echo "<td><strong>Unit Price</strong></td>";
        echo "<td><strong>Total Price</strong></td>";
        echo "<td></td>";
        echo "</tr>";
        while($itemsrow = mysql_fetch_assoc($itemsres)) {
            $quantitytotal =
            $itemsrow['price'] * $itemsrow['quantity'];
            echo "<tr>";
            if(empty($itemsrow['image'])) {
                echo "<td><img src='productimages/dummy.jpg' width='50' alt='" . $itemsrow['name'] . "'></td>";
            }
            else {
                echo "<td><img src='productimages/" .$itemsrow['image'] . "' width='50' alt='". $itemsrow['name'] . "'></td>";
            }
            echo "<td>" . $itemsrow['name'] . "</td>";
            echo "<td>" . $itemsrow['quantity'] . "</td>";
            echo "<td><strong>&pound;" . sprintf('%.2f', $itemsrow['price']) . "</strong></td>";
            echo "<td><strong>&pound;". sprintf('%.2f', $quantitytotal) . "</strong></td>";
            echo "<td>[<a href='delete.php?id=". $itemsrow['itemid'] . "'>X</a>]</td>";
            echo "</tr>";
            @$total = $total + $quantitytotal;
            $totalsql = "UPDATE orders SET total = ". $total . " WHERE id = ". $_SESSION['SESS_ORDERNUM'];
            $totalres = mysql_query($totalsql);
        }
        echo "<tr>";
        echo "<td></td>";
        echo "<td></td>";
        echo "<td></td>";
        echo "<td>TOTAL</td>";
        echo "<td><strong>&pound;". sprintf('%.2f', $total) . "</strong></td>";
        echo "<td></td>";
        echo "</tr>";
        echo "</table>";
        echo "<p><a href='checkout-address.php'>Go to the checkout</a></p>";
    }
}

?>

注意:未定义的索引:第 15 行 C:\xampp\htdocs\PHPExercise\go4shop\addtobasket.php 中的 SESS_ORDERNUM

注意:未定义索引:第 20 行 C:\xampp\htdocs\PHPExercise\go4shop\addtobasket.php 中的 SESS_LOGGEDIN

致命错误:在第 31 行调用 C:\xampp\htdocs\PHPExercise\go4shop\addtobasket.php 中未定义的函数 session_register()

添加到basket.php

<?php
session_start();
require("config.php");
require("functions.php");
$validid = pf_validate_number($_GET['id'],"redirect", $config_basedir);
$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$prodres = mysql_query($prodsql);
$numrows = mysql_num_rows($prodres);
$prodrow = mysql_fetch_assoc($prodres);
if($numrows == 0){
    header("Location: " . $config_basedir);
}
else{
    if($_POST['submit']) {
        if($_SESSION['SESS_ORDERNUM']) {
            $itemsql = "INSERT INTO orderitems(order_id,product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM'] . ", ". $_GET['id'] . ", ". $_POST['amountBox'] . ")";
            mysql_query($itemsql);
        }
        else {
            if($_SESSION['SESS_LOGGEDIN']) {
                $sql = "INSERT INTO orders(customer_id,registered, date) VALUES(". $_SESSION['SESS_USERID'] . ", 1, NOW())";
                mysql_query($sql);
                session_register("SESS_ORDERNUM");
                $_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
                $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM']. ", " . $_GET['id'] . ", ". $_POST['amountBox'] . ")";
            mysql_query($itemsql);
            }
            else {
                $sql = "INSERT INTO orders(registered,date, session) VALUES(". "0, NOW(), '" . session_id() . "')";
                mysql_query($sql);
                session_register("SESS_ORDERNUM");
                $_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
                $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", ". $_POST['amountBox'] . ")";
                mysql_query($itemsql);
            }
        }
        $totalprice = $prodrow['price'] * $_POST['amountBox'] ;
        $updsql = "UPDATE orders SET total = total + ". $totalprice . " WHERE id = ". $_SESSION['SESS_ORDERNUM'] . ";";
        mysql_query($updres);
        header("Location: " . $config_basedir . "showcart.php");
    }
    else {
        require("header.php");
        echo "<form action='addtobasket.php?id=". $_GET['id'] . "' method='POST'>";
        echo "<table cellpadding='10'>";
        echo "<tr>";
        if(empty($prodrow['image'])) {
            echo "<td><imgsrc='./productimages/dummy.jpg' width='50' alt='". $prodrow['name'] . "'></td>";  
        }
        else {
            echo "<td><img src='./productimages/" . $prodrow['image']. "' width='50' alt='" . $prodrow['name']. "'></td>";
        }
        echo "<td>" . $prodrow['name'] . "</td>";
        echo "<td>Select Quantity <select name='amountBox'>";
        for($i=1;$i<=100;$i++)  {
            echo "<option>" . $i . "</option>";
        }
        echo "</select></td>";
        echo "<td><strong>&pound;". sprintf('%.2f', $prodrow['price']) . "</strong></td>";
        echo "<td><input type='submit' name='submit' value='Add to basket'></td>";
        echo "</tr>";
        echo "</table>";
        echo "</form>";
    }
}

?>

4

2 回答 2

0

在 addbasket.php 中解决的错误。

访问会话值,不检查是否设置。

以下是代码:

<?php
session_start();
require("config.php");
require("functions.php");
$validid = pf_validate_number($_GET['id'],"redirect", $config_basedir);
$prodsql = "SELECT * FROM products WHERE id = " . $_GET['id'] . ";";
$prodres = mysql_query($prodsql);
$numrows = mysql_num_rows($prodres);
$prodrow = mysql_fetch_assoc($prodres);
if($numrows == 0){
    header("Location: " . $config_basedir);
}
else{
    if($_POST['submit']) {
        if(isset($_SESSION['SESS_ORDERNUM'])) {
            $itemsql = "INSERT INTO orderitems(order_id,product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM'] . ", ". $_GET['id'] . ", ". $_POST['amountBox'] . ")";
            mysql_query($itemsql);
        }
        else {
            if(isset($_SESSION['SESS_LOGGEDIN'])) {
                $sql = "INSERT INTO orders(customer_id,registered, date) VALUES(". $_SESSION['SESS_USERID'] . ", 1, NOW())";
                mysql_query($sql);
                //session_register("SESS_ORDERNUM");
                $_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
                $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM']. ", " . $_GET['id'] . ", ". $_POST['amountBox'] . ")";
            mysql_query($itemsql);
            }
            else {
                $sql = "INSERT INTO orders(registered,date, session) VALUES(". "0, NOW(), '" . session_id() . "')";
                mysql_query($sql);
                //session_register("SESS_ORDERNUM");
                $_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
                $itemsql = "INSERT INTO orderitems(order_id, product_id, quantity) VALUES(". $_SESSION['SESS_ORDERNUM'] . ", " . $_GET['id'] . ", ". $_POST['amountBox'] . ")";
                mysql_query($itemsql);
            }
        }
        $totalprice = $prodrow['price'] * $_POST['amountBox'] ;
        $updsql = "UPDATE orders SET total = total + ". $totalprice . " WHERE id = ". $_SESSION['SESS_ORDERNUM'] . ";";
        mysql_query($updres);
        header("Location: " . $config_basedir . "showcart.php");
    }
    else {
        require("header.php");
        echo "<form action='addtobasket.php?id=". $_GET['id'] . "' method='POST'>";
        echo "<table cellpadding='10'>";
        echo "<tr>";
        if(empty($prodrow['image'])) {
            echo "<td><imgsrc='./productimages/dummy.jpg' width='50' alt='". $prodrow['name'] . "'></td>";  
        }
        else {
            echo "<td><img src='./productimages/" . $prodrow['image']. "' width='50' alt='" . $prodrow['name']. "'></td>";
        }
        echo "<td>" . $prodrow['name'] . "</td>";
        echo "<td>Select Quantity <select name='amountBox'>";
        for($i=1;$i<=100;$i++)  {
            echo "<option>" . $i . "</option>";
        }
        echo "</select></td>";
        echo "<td><strong>&pound;". sprintf('%.2f', $prodrow['price']) . "</strong></td>";
        echo "<td><input type='submit' name='submit' value='Add to basket'></td>";
        echo "</tr>";
        echo "</table>";
        echo "</form>";
    }
}
于 2013-10-01T17:29:53.667 回答
0

这是因为您正在比较$error并且未设置其值。

请参考以下代码:

function pf_validate_number($value, $function, $redirect) {
  $error = 0; // Initialize the variable first.
    if(isset($value) == TRUE) {
        if(is_numeric($value) == FALSE) {
            $error = 1;
        }
        if($error == 1) {
            header("Location: " . $redirect);
        }
        else {
            $final = $value;
        }
    }
    else {
        if($function == 'redirect') {
            header("Location: " . $redirect);   
        }
        if($function == "value") {
            $final = 0;
        }
    }
    return $final;
}
于 2013-10-01T17:19:46.143 回答