0

So im making an online store. index.php and view.php files are fine. When I click on the product in my index.php it properly shows its information in the view.php file. But when i click Buy Now on my view.php file, its supposed to open the cart.php file with the products information . But I get the following error: Notice: Undefined index: product in /storage/ssd3/036/3764036/public_html/carrillo/cart.php on line 6 How can I solve it and why does it happen? Here is my cart.php code:

<!doctype html>
<html>
<head>
<?php
include('config.php');
$productid=$_GET['product'];
?>
<meta charset="utf-8">
<title>Cart</title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed|Rubik" rel="stylesheet">
<style>
#undr{width:100%; height:580px; position:absolute; top:75px; left:0px;}
.bdimg{width:100%; height:auto}
.big-outer{ width:80%; height:100%; background:rgba(255,255,255,0.7); margin:auto}
.big-outer p{ font-size:60px; text-align:center; margin:0px;}
.upper-details{background:#EFEFEF;}
.upper-details td{text-align:center;}
td{text-align: center;}
#emptycart{font-size:20px;margin-bottom:15px;color:#111; float:right}
#emptycart:hover{ color:#fff}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
 <header>
  <?php
   include('head.php');
  ?>
 </header>
 
<div id="undr">
  
 </div>
<?php
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
 $wasFound = false;
 $i = 0;
 if (!isset($_SESSION["cartshop"]) || count($_SESSION["cartshop"]) < 1) { 
  $_SESSION["cartshop"] = array(0 => array("item_id" => $pid, "quantity" => 1));
 } else {
  foreach ($_SESSION["cartshop"] as $each_item) { 
        $i++;
        while (list($key, $value) = each($each_item)) {
      if ($key == "item_id" && $value == $pid) {
       array_splice($_SESSION["cartshop"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
       $wasFound = true;
      }
        }
        }
     if ($wasFound == false) {
      array_push($_SESSION["cartshop"], array("item_id" => $pid, "quantity" => 1));
     }
 }
 header("location: cart.php"); 
    exit();
}
?>
<?php
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cartshop"]);
}
?>
<?php
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
 $item_to_adjust = $_POST['item_to_adjust'];
 $quantity = $_POST['quantity'];
 $quantity = preg_replace('#[^0-9]#i', '', $quantity);
 if ($quantity >= 11) { $quantity = 10; }
 if ($quantity < 1) { $quantity = 1; }
 if ($quantity == "") { $quantity = 1; }
 $i = 0;
 foreach ($_SESSION["cartshop"] as $each_item) { 
        $i++;
        while (list($key, $value) = each($each_item)) {
      if ($key == "item_id" && $value == $item_to_adjust) {
       array_splice($_SESSION["cartshop"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
      }
        }
 }
}
?>
<?php
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
  $key_to_remove = $_POST['index_to_remove'];
 if (count($_SESSION["cartshop"]) <= 1) {
  unset($_SESSION["cartshop"]);
  header('Location: ' . $_SERVER['HTTP_REFERER']);
 } else {
  unset($_SESSION["cartshop"]["$key_to_remove"]);
  sort($_SESSION["cartshop"]);
  header('Location: ' . $_SERVER['HTTP_REFERER']);
 }
}
?>
    <div id="lowrbdy">
   <div class="big-outer">
     <p style="text-decoration:underline">Cart</p>
<table width="100%" border="0" style="border-collapse:collapse">
<?php
if(isset($_SESSION['cartshop'])==!NULL){
?>
  <tbody>
    <tr class="upper-details">
      <td height="36" colspan="2" style="border-right: 1px solid #000;">Item</td>
      <td width="16%" style="border-right: 1px solid #000;">Quantity</td>
      <td width="16%" style="border-right: 1px solid #000;">Unit Price</td>
      <td width="21%">Sub Total</td>
    </tr>
<?php
}
?>
<?php 
$cartTotal = "";
if (!isset($_SESSION["cartshop"]) || count($_SESSION["cartshop"]) < 1) {
    echo '<div class="empty-cart"><h2 class="crta">Your Shopping Cart Is Empty</h2>';
 echo '<br><a href="index.php"><h2 class="alink">Continue Shopping</h2></a></div>';
} else {
$i = 0; 
    foreach ($_SESSION["cartshop"] as $each_item) { 
  $item_id = $each_item['item_id'];
  $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1");
  while ($row = $row = $sql->fetch_assoc()) {
   $productname = $row["name"];
   $producttotalprice = $row["price"];
   $productcode = $row["id"];
   $pic=$row['pro_image'];
   $pdelc=$row['pdelc'];
   $pr=$row['price'];
  }
  $producttotalpricetotal = $producttotalprice * $each_item['quantity'];
  $cartTotal = $producttotalpricetotal + $cartTotal;
echo'<tr>
      <td width="7%" rowspan="3" style="border-bottom: 2px solid #000; height:100px"><img src="imeg/'.$pic.'"/></td>
      <td width="29%" height="21" style="border-right: 1px solid #000;">&nbsp;</td>
      <td rowspan="2" style="border-right: 1px solid #000;">
    <form action="cart.php" method="post">
     <input name="quantity" id="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" class="qnttxt"/></br>
   <input id="adjustBtn" name="adjustBtn' . $item_id . '" type="submit" value="Update" class="qntbtn"/>
   <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
    </form>
   </td>
      <td style="border-right: 1px solid #000;">&nbsp;</td>
      <td>&nbsp;</td>
      </tr>
    <tr style="border-bottom: 2px solid #000;">
      <td style="border-right: 1px solid #000;">'.$productname.'</td>
      <td style="border-right: 1px solid #000;">Rs. '.number_format($producttotalprice).'</td>
      <td><p style="float:left;margin:0px 0px 0px 20px;font-size:18px;text-decoration:none">Rs. '.number_format($producttotalprice*$each_item['quantity']).'</p>
    <form action="cart.php" method="post">
   <input name="deleteBtn' . $item_id . '" type="submit" value="X" class="removebtn"/>
   <input name="index_to_remove" type="hidden" value="' . $i . '" />
        </form>
   </td>
    </tr>
    <tr>';
 $i++;
 }
echo'<div style="width:400px; height:40px; background:rgba(100,190,255,1.00); margin:auto; margin-bottom:6px;margin-top:10px">
<p style="font-size:20px;text-align:center; color:#fff; line-height:2em">Cart Total: <strong>Rs. '.number_format($cartTotal).' /-</strong></p></div>
<a href="clear-session.php"><div style="width:55%; height:22px;"><p id="emptycart">( Empty Cart )</p></div></a>
';
}
?>
  </tbody>
</table>
        </div>
    </div>
</body>
</html>
Here is my view.php code:

<!doctype html>
<html>
<head>
<?php

$mysqli = new mysqli("localhost", "id3764036_alan", "agro12345", "id3764036_agrotienda");


$productid=$_GET['product'];
?>
<meta charset="utf-8">
<title>View Product <?=$productid?></title>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed|Rubik" rel="stylesheet">
<style>
#undr{width:100%; height:580px; position:absolute; top:75px; left:0px;}
.bdimg{width:100%; height:100%}
.big-outer{ width:80%; height:100%; background:rgba(255,255,255,0.7); margin:auto}
.big-outer p{text-align:center; font-size:40px; margin:10px auto}
.outer{ width:270px; height:310px;  margin:auto;}
.outer img {width:88% !important;}
.price{text-align:center; margin:20px auto; background:#16B472; color:#fff; width:30%; padding:5px 0px;}
.price p{margin:0px; font-size:26px;}
.buy{text-align:center; margin:20px auto; background:#3E8BDC; width:34%; padding:5px 0px; cursor:pointer}
.buy:hover{ background:#2E5AE4;transition:all 0.4s ease-in-out}
#subaz{border:none; background-color:transparent; font-size:32px; color:#fff; font-weight:bold; cursor:pointer}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
 <header>
  <?php
   include('head.php');
  ?>
 </header>
 
    <div id="undr">
  
 </div>
<?php
$select_query="select * from product where id='$productid'";
$sql=mysqli_query($mysqli,"select * from product where id='$productid'");

$row=$row = $sql->fetch_assoc();
?>
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?= $productid ?>" />
    <div id="lowrbdy">
  <div class="big-outer">
         <div class="outer">
             <img src="imeg/<?=$row["pro_image"]?>"/>
            </div>
            <p><?=$row["name"]?></p>
            <div class="price"><p>₡ <?=$row["price"]?></p></div>
            <div class="buy"><input type="submit" id="subaz" value="Buy Now"/></div>
        </div>
    </div>
</form>
</body>
</html>

1 Answers1

-1

Cart.php :

$productid=$_GET['product'];

But the request provide 'pid' argument.

View.php:

<input type="hidden" name="pid" id="pid" value="<?= $productid ?>" />

So you have 2 solutions:

1- edit line 6 in cart.php to match the argument pid like

$productid=$_POST['pid'];

2- or edit the name of your input in view.php like:

<input type="hidden" name="product" id="pid" value="<?= $productid ?>" />

And change method to GET in view.php, or use $_POST[] instead of $_GET[] in cart.php

tdjprog
  • 706
  • 6
  • 11