2013-02-15 286 views
-1

我的PHP代碼有一些問題。對於變量「$ total」,我有一個未定義的索引錯誤。我需要一些幫助來解決這個問題。這是我的整個PHP代碼。PHP代碼中未定義的索引?

<?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 = $_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'] [email protected]$_SESSION['cart_'.$add_id]){ 
       @$_SESSION['cart_'.$_GET['add']]+='1'; 
       header('Location: index.php'); 
      } 
      else{ 
       header('Location: index.php?err=max'); 
      } 
     } 

    } 

    if(isset($_GET['remove'])){ 
     $_SESSION['cart_'.(int)$_GET['remove']]--; 
     header("Location: index.php"); 
    } 

    if(isset($_GET['delete'])){ 
     $_SESSION['cart_'.(int)$_GET['delete']]='0'; 
     header('Location: index.php'); 
    } 

    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 = 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).' = $'.$sub.' <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?delete='.$id.'">[Delete]</a>'; 
        } 
       } 
       $total += $sub; 
      } 
     } 
     echo '<br />'.$total; 
    } 

    ?> 
</body> 

在此先感謝您的幫助。在不久的將來我可能需要更多的幫助。

+2

哦,男孩。您的代碼易受SQL注入攻擊。我希望你不打算在生產中使用它。你也應該停止使用'mysql_'函數。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-02-15 22:45:56

+0

哦。感謝您的建議。 – WorldxFree 2013-02-15 22:48:54

+1

查看[誰是Bobby Tables?](http://bobby-tables.com/)瞭解更多信息。 – Kermit 2013-02-15 22:49:41

回答

1

代碼試圖做的第一次$total+=$sub$total未定義。

您需要在cart()函數的頂部設置$total

function cart(){ 
    $total = 0; 
    /// rest of code 
} 
+0

我認爲這裏的問題是它超出了範圍。 – Kermit 2013-02-15 22:51:17

1

在你的函數購物車():

聲明變量$total第一。

例如$total = 0 ; ,然後開始遞增它$total += $sub ;

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 = $_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'] [email protected]$_SESSION['cart_'.$add_id]){ 
       @$_SESSION['cart_'.$_GET['add']]+='1'; 
       header('Location: index.php'); 
      } 
      else{ 
       header('Location: index.php?err=max'); 
      } 
     } 

    } 

    if(isset($_GET['remove'])){ 
     $_SESSION['cart_'.(int)$_GET['remove']]--; 
     header("Location: index.php"); 
    } 

    if(isset($_GET['delete'])){ 
     $_SESSION['cart_'.(int)$_GET['delete']]='0'; 
     header('Location: index.php'); 
    } 

    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(){ 
    $total = 0; 
     foreach($_SESSION as $name => $value){ 
      if($value>0){ 
       if(substr($name, 0, 5)=='cart_'){ 
        $id = 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).' = $'.$sub.' <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?delete='.$id.'">[Delete]</a>'; 
        } 
       } 
       $total += $sub; 
      } 
     } 
     echo '<br />'.$total; 
    } 

    ?> 
</body> 

修正了你的代碼。