2014-04-20 5 views
0

36 나는 간단한 PHP 체크 아웃을 가지고 있지만 나는 다음과 같은 오류 얻을 장바구니에 제품을 추가 할 때 :PHP주의 사항은 : 오프셋 정의되지 않은 :

PHP 공지 사항을 : 오프셋 정의되지 않은 : C 36 : \을 Inetpub \ wwwroot에 \ 쇼핑 \ cart.php on line 76

나는 페이지를 새로 고치거나 다른 제품을 추가하기 위해 돌아가서 제품을 잘 나타내면 카트에 첫 제품을 추가 할 때이 오류가 발생하는 것 같습니다. 어떤 도움을 시도해도 모든 대답이 효과가 없었기 때문에 많은 도움을받을 수있었습니다.

<?php 
         // connect to the database 
         include("databasedrop.php"); 
?> 


         <div class="post"> 
          <h2>Shopping Basket<span class="title-bottom">&nbsp;</span></h2> 

         </div> 
         <!-- End Post --> 

       <?php 
       if (empty($_GET['id'])) { 
        $_GET['id'] = ""; 
       } 


       $ProductID = $_GET['id']; //the product id from the URL 
       $action = $_GET['action']; //the action from the URL 
       //if there is an product_id and that product_id doesn't exist display an error message 


       switch ($action) { //decide what to do 
        case "add": 
         $_SESSION['cart'][$ProductID] ++; //add one to the quantity of the product with id $product_id 
         break; 

        case "remove": 
         $_SESSION['cart'][$ProductID] --; //remove one from the quantity of the product with id $product_id 
         if ($_SESSION['cart'][$ProductID] == 0) 
          unset($_SESSION['cart'][$ProductID]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
         break; 

        case "empty": 
         unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
         break; 
       } 
       ?>    


       <?php 
       if (empty($_SESSION['cart'])) { 
        $_SESSION['cart'] = ""; 
       } 
       if ($_SESSION['cart']) { //if the cart isn't empty 
        //show the cart 
        echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table 
        //iterate through the cart, the $product_id is the key and $quantity is the value 
        foreach ($_SESSION['cart'] as $ProductID => $quantity) { 

         //get the name, description and price from the database - this will depend on your database implementation. 
         //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection 
         if ($stmt = $conn->prepare("SELECT * FROM products WHERE ProductID=?")) { 
          $stmt->bind_param("i", $ProductID); 
          $stmt->execute(); 

          $stmt->bind_result($ProductID, $Title, $Description, $Price, $Stock, $Image, $Category, $Status); 
          $stmt->fetch(); 

          // show the form 
         } 



         $total = ""; 
         $line_cost = $Price * $quantity; //work out the line cost 
         $total = $total + $line_cost; //add to the total cost 

         echo "<tr>"; 
         //show this information in table cells 
         echo "<td align=\"center\">$Title</td>"; 
         //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product 
         echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$ProductID\">X</a></td>"; 
         echo "<td align=\"center\">$line_cost</td>"; 

         echo "</tr>"; 
        } 

        //show the total 
        echo "<tr>"; 
        echo "<td colspan=\"2\" align=\"right\">Total</td>"; 
        echo "<td align=\"right\">$total</td>"; 
        echo "</tr>"; 

        //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation 
        echo "<tr>"; 
        echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; 
        echo "</tr>"; 
        echo "</table>"; 
       } else { 

        //otherwise tell the user they have no items in their cart 
        echo "You have no items in your shopping cart."; 
       } 


       ?> 
+0

하는 선이 76입니까? –

+0

[PHP : "Notice : 정의되지 않은 변수"및 "Notice : Undefined index"] 복제본 (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) –

+0

코드가 너무 깁니다. 불필요한 부분을 제거하고 오류를 생성하는 깔끔한 예를 제공하십시오. – NKN

답변

1

어둠 속에서 쐈지 만 볼 수있는 해결책이 있습니다.

$_SESSION['cart'] 배열을 만들었습니까?

이 시도 :

switch ($action) { 
    case "add": 
    if (!isset($_SESSION['cart'][$ProductID])) { 
     $_SESSION['cart'][$ProductID] = 1; 
    } 
    else { 
     $_SESSION['cart'][$ProductID]++; 
    } 
    break; 
관련 문제