-2

I have a function in PHP that prints a list of inventories with their IDs.

Another function lists the products in the inventory with the corresponding ID from the inventory list.

But I don't know how to get the inventory ID from the list correctly into the product list, and it doesn't work for me. I don't know exactly where I'm doing wrong. When I list the products with a specific ID and refresh the page, I want the list to stay the same.

A function to get a list

    <?
    php function fetchSeznamInventur() {
        global $userCon;
        global $rowcount;
        global $query;
        global $inventuraId;
        global $row;
        $query = mysqli_query($userCon, "SELECT * FROM seznamInventur");
        //$inventuraId = $row["id"];
        $rowcount = mysqli_num_rows($query);
        
    }  
?>

List listing to HTML

        <?php
        <table border="1" class="invTable">
            <thead>
                <th>Datum</th>
                <th>ID</th>
                <th>Název</th>
            </thead>
           <?php 
            
            
        for ($inv = 1; $inv <= $rowcount; $inv++) {
            $row = mysqli_fetch_array($query);
            ?>
            <tr>
                <td><?php echo $row["createdate"]?></td>
                <td name="id"><?php echo $row["id"] ?></td>
                <td><?php echo $row["name"]?></td>
                <td><input type="submit" name="submit" value="Detail"></td>
            </tr>
        <?php
        }
        ?>
    
        
        </table>
    ?>

Obtaining products by inventory ID

<?php
function fetchInventura() {
    global $userCon;
    global $inventuraId;
    global $rowcount;
    global $query;
    $query = mysqli_query($userCon, "SELECT * FROM inv WHERE inventuraId='$inventuraId'");
    $rowcount = mysqli_num_rows($query);
    echo $inventuraId;
}
?>

List of products in html

<?php
for ($products = 1; $products <= $rowcount; $products++) {
    $row = mysqli_fetch_array($query);

    ?> 
    <tr>
        <td><?php echo $row["id"]?></td>
        <td><?php echo $row["inventuraId"] ?></td>
        <td style="display: none;"><?php echo $row["name"]?></td>
        <td><?php echo $row["ean"]?></td>
        <td style="display: none;"><?php echo $row["plu"]?></td>
        <td style="display: none;"><?php echo $row["externalId"]?></td>
        <td style="display: none;"><?php echo $row["productId"]?></td>
        <td><?php echo $row["quantity"]?></td>
        <td><?php echo $row["versiondate"]?></td>
    </tr>
<?php
}

?>
    
    </table>

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Please revise your question's title to be descriptive -- not keywords. That's what tags are for – j08691 Nov 30 '22 at 14:49
  • Make sure you use ` – Ken Lee Nov 30 '22 at 14:58
  • `` won't do much, as it's not inside a form. You probably need a hyperlink which goes to a "products" page and passes the inventory ID as a query parameter, e.g. `">Display`. And then on the products page, you'd use `$_GET["inventoryID"]` to get the ID, and pass that into your SQL query (but please use a parameter, don't inject variables directly into the SQL, it's a security risk). – ADyson Nov 30 '22 at 15:00
  • Make sure you do NOT overuse the `global` keyword, only use it in those place(s) which are needed. – Ken Lee Nov 30 '22 at 15:02
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Nov 30 '22 at 15:14
  • @ADyson I am a newbie and this is exactly the answer I needed. I knew I had to send that ID from the table somehow. But I am very lost on how to get that ID on the page where I want to list it. And your answer is great. Thank you very much – Jiří Liška Dec 01 '22 at 08:36
  • @KenLee Yes, I discovered writing functions in one file and then calling them. I kind of fell in love with it and started writing like that. But thanks for the info. I'll go through the features and check if I'm really overusing them. – Jiří Liška Dec 01 '22 at 08:39
  • @ADyson Thank you very much. I got the ID using GET as you wrote and it already works. – Jiří Liška Dec 01 '22 at 08:49

1 Answers1

0

<input type="submit" name="submit" value="Detail">

won't do much, as it's not inside a form.

You probably need a hyperlink which goes to a "products" page and passes the inventory ID as a query parameter, e.g.

<a href="productsByInventory.php?inventoryID=<?php echo $row["id"]; ?>">Display</a>

And then on the products page, you'd use $_GET["inventoryID"] to get the ID, and pass that into your SQL query (but please use a parameter, don't inject variables directly into the SQL, it's a security risk).

ADyson
  • 57,178
  • 14
  • 51
  • 63