soldiershin
soldiershin

Reputation: 1620

Pass current id in the next page in php

I am developing a restaurant menu system in which there is a single menu.A menu has many categories and each category has multiple items in it.Now i have created the create , show functionality of Categories and the show functionality of the Item but i am stuck on the create functionality of the item because i need the category's id to insert item in that particular id.This is my code for showing Items in a category.

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link   href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
        <div class="row">
            <h3>Category Items</h3>
        </div>
        <div class="row">
        <p> <a href="create_items.php" class="btn btn-success">Create</a> </p>
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>Name</th>
                  <th>Description</th>
                  <th>Price</th>
                </tr>
              </thead>
              <tbody>
              <?php
              require 'database.php';
                $id = null;
                if ( !empty($_GET['id'])) {
                    $id = $_REQUEST['id'];
                }

                if ( null==$id ) {
                    header("Location: index.php");
                } else {
               $pdo = Database::connect();
               $sql = "SELECT * FROM Items where I_C_id = '" . $id . "'";
               $result = $pdo->query($sql);
               foreach ($pdo->query($sql) as $row) {
                        echo '<tr>';
                        echo '<td>'. $row['I_id'] . '</td>';
                        echo '<td>'. $row['I_name'] . '</td>';
                        echo '<td>'. $row['I_desc'] . '</td>';
                        echo '<td>'. $row['I_price'] . '</td>';
                        echo '</tr>';
               }
           }
               Database::disconnect();
              ?>
              </tbody>
        </table>
    </div>
</div> <!-- /container -->

This file gets the "id" of the category and displays the items in it of that particular category.Now i want to create items of that particular id.how should i pass the id of the category in the create page?

Upvotes: 0

Views: 1389

Answers (2)

Rob G
Rob G

Reputation: 612

Either by using PHP sessions, or by appending the id to the link, so

<a href="create_items.php"

becomes

<a href="create_items.php?cat_id=<?=$_GET['id']?>"

Unrelated to the question, but you'll want to look into avoiding SQL injection attacks, e.g. How can I prevent SQL injection in PHP?

Upvotes: 1

Halayem Anis
Halayem Anis

Reputation: 7785

You can use $_SESSION[] to store all informations related to the current client.
Sessions's data will be automatically destroyed once if client is disconnected, or no more active in your web site.
There are some configurations to do in php.ini : session time out, session folder, garbage collector probability... or you can let the default configurations
for more informations : http://php.net/manual/en/reserved.variables.session.php

Hope that helps :)

Upvotes: 0

Related Questions