Click here to Skip to main content
15,743,541 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a PHP page where users can search for food using an API and then display the food information and calories and then click a button to add the food information to their day which is another PHP file and also stores them in the database however what I want to make is to keep that data in the other PHP file I know how to pass them with GET method but it does not store them there the GET value is updated each time I add food

What I have tried:

<?php if (isset($data)){?> //$data is the API response
  <div class="card text-center" style="margin:auto; width:50%; margin-top:100px; min-height:200px">
  <div class="card-header">
  <?php foreach ($data["items"] as $item){
      echo "". "food name:  ". $item['name'] . "<br>";
      $name=$item['name'];} ?>
  <div class="card-body">
    <p class="card-text"> <?php
  foreach ($data["items"] as $item)
      echo "Serving size: " . $item["serving_size_g"] . " gram  <br>";
      echo "Calories: ". $item["calories"] . "<br>";
      echo "Protein: ". $item["protein_g"] . " gram  <br>";
      echo "Carbohydrates: ". $item["carbohydrates_total_g"] . " gram  <br>";
      echo "Fat: ". $item["fat_total_g"] . " gram  <br>";

    $name=$data['items'][0]['name'];   ?></p>
   <button><a href="addfood.php?name=<?=$name?>">Add Food</a></button>

  <div class="card-footer text-muted">
<?php } ?>
Updated 11-Aug-22 23:33pm

1 solution

You pass the data using either the querystring (a GET request) or a <form> (a POST request). Each request will only include the data you specify; data from previous requests will not be remembered.

Your question appears to be about how to store the values that are passed to the other page. But since you haven't provided any details of that page, nor shown us any code from it, we can't tell you what you need to change.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900