Search code examples
phpgetcart

Shopping Cart $_GET problem can't work with the id


I have a problem with $_GET.

On my index.php file I have a little shopping site where I can buy random stuff by clicking the "buy" button. After clicking the "buy" button I am trying to get the id of the clicked product like this:

//Stuff like connection, sql, etc..

$response="";

    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        //Other Stuff...
        $response .= "<a href='addtocart.php?id=". $row['ProduktID'] . "'><button class='inCart'>Buy</button></a>";
    }

And then I have a new file called addtocart.php. I can see the id's:

Image

Image

addtocart.php:

<?php
  session_start();

  if(isset($_GET['ProduktID']) && !empty($_GET['ProduktID'])){
     //Do Stuff
  }
  else{
     echo "Error, can't add the item";
  }

I am always getting the Error message..


Solution

  • Here addtocart.php?id you have used id, so in URL it will passed param as id

         $response .= "<a href='addtocart.php?id=". $row['ProduktID'] . "'><button class='inCart'>Buy</button></a>";
    

    addtocart.php:

    so in php you should access as $_GET['id']

    <?php
      session_start();
    
      if(isset($_GET['id']) && !empty($_GET['id'])){
         //Do Stuff
      }
      else{
         echo "Error, can't add the item";
      }