1. ホーム
  2. php

[解決済み] ecommerce website "add to cart" button it show message Undefined index: id in <b>C:\xampp\htdocs\online shop\index.php</b> on line <b>147</b><br />

2022-03-01 22:17:43

Question

I want to add "add to cart" button in my index.php page under every products. But "add to cart" button it show message: Undefined index: id in C:\xampp\htdocs\online shop\index.php on line 147
. need to solve this.

index.php

 <!--Product Show Start-->  
 <div class="w3-container w3-card-2 w3-white w3-margin-bottom ">
 <div class="container"></br>
 <div id="result_div" class="result_div"></div>
<h1><b>New Products:</b></h1>
<div class="row">

 <?php
    $per_page = 8;
        $p = new Product();
        $data = $p->View();

        if(!isset($_GET['page']))
        {
            $start = 0;
            $end = $per_page;
        }
        else
        {
            $start = ($_GET['page'] - 1) * $per_page;
            $end = $_GET['page'] * $per_page;
        }
        if($end>count($data))
        {
            $end = count($data);
        }

        for($i=$start; $i<$end; $i++)
        {
    ?>

    <div class="col-md-3" >
          <div class="thumbnail" style="box-shadow: 4px 4px 4px 4px  #888888;">
            <a href="user/details.php?id=<?php print $data[$i][0];?>">
            <img style="width:200px;height:150px" src="image/<?php print  $data[$i][5]?>"/></a>
            <div class="caption">
              <h4><a href="user/details.php?id=<?php print $data[$i] [0];?>"><?php print $data[$i][1]?></a></h4>
              <h4>Price: <?php print $data[$i][2]?> TK</h4>


            </div>

            <div class="space-ten"></div>
            <div class="btn-ground text-center">

                <a class="btn btn-success" href="user/add_to_cart.php?id=<?php print $_GET['id'];?>">Add to cart</a>
                <a class="btn btn-primary"  href="user/details.php?id=<?php print $data[$i][0];?>">View</a>
            </div>
            <div class="space-ten"></div>
          </div>
        </div>
     <?php
    }
   ?>       

 </div>
  <!--pagination start-->
  <style>
 .pagination>li>a, .pagination>li>span { border-radius: 50%   !important;margin: 0 5px;}
 </style>
  <ul class="pagination">
          <li><a href="#">«</a></li>

          <li><?php
                $p = 1;
                for($i=0; $i<count($data); $i = $i+$per_page)
                {
                    echo "<a href=\"index.php?page={$p}\" class='page'>".   $p ."</a>";
                    $p++;
                }
            ?></li>

          <li><a href="#">»</a></li>
        </ul>
  <!--pagination end-->         
  </div>

   </div>
  <!--Product Show End-->

add_to_cart.php

<?php
require_once("../dataAccessLayer/dalSession.php");

Session::Start();

if(isset($_GET['id']))
{

if(isset($_SESSION['cart']))
{
    $c=0;
    for($i=0; $i<count($_SESSION['cart']); $i++)
    {   
        if($_SESSION['cart'][$i] == $_GET['id'])
        {
            $c++;
            break;
        }   
    }
    if($c == 0)
    {
        $_SESSION['cart'][] = $_GET['id'];
        $_SESSION['qty'][] = 1;
    }

  }
  else
  {
    $_SESSION['cart'][] = $_GET['id'];
    $_SESSION['qty'][] = 1;     
  }

  }
  header("Location: details.php?id={$_GET['id']}");

 ?>

cart.php

 <?php
 require_once("../dataAccessLayer/dalSession.php");
 require_once("../dataAccessLayer/dalComment.php");
 require_once("../dataAccessLayer/dalProduct.php");
 ?>

<?php
                     $pro = new Product();
                     $pro->product_id = $_GET['id'];
                     $r = $pro->SelectById();
                    ?>

解決方法は?

index.phpにあるこのコードを置き換えるだけです。

<!--Product Show Start-->   
 <div class="w3-container w3-card-2 w3-white w3-margin-bottom ">
 <div class="container"></br>
 <div id="result_div" class="result_div"></div>
<h1><b>New Products:</b></h1>
<div class="row">

 <?php
    $per_page = 8;
        $p = new Product();
        $data = $p->View();

        if(!isset($_GET['page']))
        {
            $start = 0;
            $end = $per_page;
        }
        else
        {
            $start = ($_GET['page'] - 1) * $per_page;
            $end = $_GET['page'] * $per_page;
        }
        if($end>count($data))
        {
            $end = count($data);
        }

        for($i=$start; $i<$end; $i++)
        {
    ?>

    <div class="col-md-3" >
          <div class="thumbnail" style="box-shadow: 4px 4px 4px 4px  #888888;">
            <a href="user/details.php?id=<?php print $data[$i][0];?>">
            <img style="width:200px;height:150px" src="image/<?php print  $data[$i][5]?>"/></a>
            <div class="caption">
              <h4><a href="user/details.php?id=<?php print $data[$i] [0];?>"><?php print $data[$i][1]?></a></h4>
              <h4>Price: <?php print $data[$i][2]?> TK</h4>


            </div>

            <div class="space-ten"></div>
            <div class="btn-ground text-center">


              <?php
                    $_GET['id']=$data[$i][0];
                     $pro = new Product();
                     $pro->product_id = $_GET['id'];
                     $r = $pro->SelectById();

                     ?>

                <a class="btn btn-success" href="user/add_to_cart.php?id=<?php print $_GET['id'];?>">Add to cart</a>
                <a class="btn btn-primary"  href="user/details.php?id=<?php print $data[$i][0];?>">View</a>
            </div>
            <div class="space-ten"></div>
          </div>
        </div>
     <?php
    }
   ?>       

 </div>
  <!--pagination start-->
  <style>
 .pagination>li>a, .pagination>li>span { border-radius: 50%   !important;margin: 0 5px;}
 </style>
  <ul class="pagination">
          <li><a href="#">«</a></li>

          <li><?php
                $p = 1;
                for($i=0; $i<count($data); $i = $i+$per_page)
                {
                    echo "<a href=\"index.php?page={$p}\" class='page'>".   $p ."</a>";
                    $p++;
                }
            ?></li>

          <li><a href="#">»</a></li>
        </ul>
  <!--pagination end-->         
  </div>

   </div>
  <!--Product Show End-->