2016-09-17 39 views
0

我想用php做一個購物車。我想發送id到下面的文件中的php代碼。我試圖用php_self來做到這一點。我得到這些錯誤。如何通過超鏈接發送數據到php文件本身

*Undefined index: movie_id in C:\xampp\htdocs\ITA_WEBSITE\shopping_cart.php on line 60 
*mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\ITA_WEBSITE\shopping_cart.php on line 61 
*Undefined index: cart in C:\xampp\htdocs\ITA_WEBSITE\shopping_cart.php on line 84 

下面的代碼

<?php 
    session_start(); 
    include('dbconfig.php'); 
    $result = mysqli_query($con,"SELECT * FROM movie_details"); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Movie Store</title> 
     <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> 
     <link rel="stylesheet" type="text/css" href="css/main.css"> 
    </head> 
    <body> 
<div class="container-fluid" id="maincontent"> 
      <div class="row"> 
       <?php while($product = mysqli_fetch_object($result)) { ?> 
         <div class="col-md-4 col-xs-12"> 
          <div class="thumbnail customsthumbs"> 
           <img src=<?php echo $product->image_path;?>    alt=<?php echo $product->image_path;?>> 
           <div class="caption"> 
            <h3 class="customh"><?php echo $product->movie_title;?></h3> 
            <p id="price">Price : $<?php echo $product->price;?></p> 
            <p style="text-align:center"><a href=<?php $_SERVER['PHP_SELF'].'?movie_id='?><?php echo $product->movie_id?> class="btn btn-primary custombtn" role="button"> 
            <span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span>Add To Cart</a> 

           </p> 
          </div> 
         </div> 
        </div> 
      <?php } ?> 
       </div> 

<?php 

      include('dbconfig.php'); 
      include('movie_products.php'); 
      $result = mysqli_query($con, 'SELECT * FROM movie_details WHERE movie_id = '.$_GET['movie_id']); 
      $product = mysqli_fetch_object($result); 
      if(isset($_GET['movie_id'])){ 
       $item = new Item(); 
       $item->movie_id = $product->movie_id; 
       $item->movie_title = $product->movie_title; 
       $item->price = $product->price; 
       $item->quantity = 1; 
       $_SESSION['shopping_cart'][] = $item; 
      } 
     ?> 

     <div class="container-fluid"> 
       <div class="row"> 
        <div id="cart"> 
         <h3 style="text-align: center;font-size: 40px;padding-top: 5px;">Cart</h3> 
         <div id="display_cart"> 
         <table border="1" id="display_cart_table"> 
          <tr> 
           <th>Movie Title</th> 
           <th>Price</th> 
           <th>Action</th> 
          </tr> 
           <?php 
            $cart = unserialize(serialize($_SESSION['cart'])); 
            for($i=0; $i<count($cart); $i++){ 
           ?> 
          <tr> 
           <td><?php echo $cart[$i]->movie_title;?></td> 
           <td><?php echo $cart[$i]->price;?></td> 
           <td></td> 
          </tr> 
          <?php } ?> 
         </table> 
         </div> 
        </div> 
       </div> 
      </div> 
    </body> 
</html> 

movie_products.php文件

 <?php 
     class Item{ 
      var $movie_id; 
      var $movie_title; 
      var $price; 
      var $quantity; 
     } 

     ?> 

注:我試圖通過以下的教程 需要一些幫助來做到這一點!

回答

2

請看下面的語句,

<p style="text-align:center"><a href=<?php $_SERVER['PHP_SELF'].'?movie_id='?> ... 
             ^see here 

你忘了在這裏使用echo。它應該是,

<p style="text-align:center"><a href=<?php echo $_SERVER['PHP_SELF'].'?movie_id='?>... 

此外,包裝你的代碼塊中的if塊中,像這樣:

// your code 

if(isset($_GET['movie_id'])){ 
    include('dbconfig.php'); 
    include('movie_products.php'); 
    $result = mysqli_query($con, 'SELECT * FROM movie_details WHERE movie_id = '.$_GET['movie_id']); 
    $product = mysqli_fetch_object($result); 
    if(isset($_GET['movie_id'])){ 
     $item = new Item(); 
     $item->movie_id = $product->movie_id; 
     $item->movie_title = $product->movie_title; 
     $item->price = $product->price; 
     $item->quantity = 1; 
     $_SESSION['shopping_cart'][] = $item; 
    } 

    ?> 
     <div class="container-fluid"> 
      <div class="row"> 
       <div id="cart"> 
        <h3 style="text-align: center;font-size: 40px;padding-top: 5px;">Cart</h3> 
        <div id="display_cart"> 
        <table border="1" id="display_cart_table"> 
         <tr> 
          <th>Movie Title</th> 
          <th>Price</th> 
          <th>Action</th> 
         </tr> 
          <?php 
           $cart = unserialize(serialize($_SESSION['cart'])); 
           for($i=0; $i<count($cart); $i++){ 
          ?> 
         <tr> 
          <td><?php echo $cart[$i]->movie_title;?></td> 
          <td><?php echo $cart[$i]->price;?></td> 
          <td></td> 
         </tr> 
         <?php } ?> 
        </table> 
        </div> 
       </div> 
      </div> 
     </div> 
    <?php 
} 
+0

謝謝你,它的工作.. – Kasun

+0

@Kasun不客氣。 :-) –

相關問題