Kasun Kasun - 2 months ago 4
PHP Question

how to send data to php file itself via a hyperlink

I am trying to make a shopping cart with php. I wanted to send id to a php code below in the file. I tried to do it with php_self. And I get these errors.

*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


Here's the code

<?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 file

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

?>


Note: I tried to do this by following a tutorial
Need some help !!

Answer

Look at the following statement,

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

You forgot to use echo here. It should be,

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

Also, wrap your code block inside an if block, like this:

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