如何通过超链接将数据发送到PHP文件本身


how to send data to php file itself via a hyperlink

我正在尝试用php制作一个购物车。我想发送id到下面的文件中的php代码。我尝试用php_self来做。我得到了这些错误

 *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;
        }
       ?>

注意:我试图通过遵循教程来做到这一点需要一些帮助!!

看下面的语句,

<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
}