如何从MySQL数据库按ID提取字段数据?

时间:2019-04-10 19:06:18

标签: php html mysql

我有两个PHP文件:itemTransaction.php和recordItemTransaction.php。我可以从itemTransaction中的表中选择要为其记录交易的行,它链接到recordItemTransaction.php中数据库中的正确行,从而生成一个表格,允许我编辑itemQuantity。我有一个表单,其中包含一个隐藏的ID字段和一个文本框,供用户输入更新的itemQuantity,并将在提交后将其提交到数据库。我想向用户显示当前itemQuantity,因此当他们编辑itemQuantity时,他们会在编辑并记录交易之前知道当前数量。

我的问题是在recordItemTransaction.php中,我无法弄清楚如何在同一文件中同时提取ID和itemQuantity的值。      这链接到recordItemTransaction.php。由于我在这里引用ID,因此可以在下一个文件中检索它。但是我无法连同ID一起检索itemQuantity。只有一个。所以,当我切换到...      ...我可以在文本框中检索itemQuantity值,但是在提交表单时,它无法确定要更新的行。

  1. itemTransaction.php

        

        $query = "SELECT * FROM `Items` WHERE `isActive` = 'Active'";
        $result = mysqli_query($con, $query);
    
            echo "<h1>Record Transaction | Items</h1>";
    
            echo "<a href='../inventoryIndex.php'><button class='button'>Back</button></a>";
    
        //Display Data
        echo "<table class='applyFont' cellspacing='0' cellpadding='0'>";
            echo "<tr>";
                echo "<th></th>";
                echo "<th>ITEM</th>";
                echo "<th>COST</th>";
                echo "<th>RECORD TRANSACTION</th>";
            echo "</tr>";
    
        while($row=mysqli_fetch_array($result)) {
            echo "<tr>";
                echo "<td align='center' width='9%'><img src='/InventoryManager/InventoryManagerImages/Items/{$row['itemImage']}' width='115' height='125' style='display:block'></td>";
                echo "<td align='center' width='30%'>{$row['description']}</td>";
                echo "<td align='center' width='30%'>$ {$row['unitCost']}</td>";
                echo "<td align='center'><a href='recordItemTransaction.php?ID={$row['ID']}'><img src='/InventoryManager/InventoryManagerImages/Icons/couple-of-arrows-changing-places.png' title='Record an update to inventory'></td>";
            echo "</tr>";
        }
        ?>
    </body>
    </html>
    
  2. recordItemTransaction.php

                                                                >                 >                                                                                                                             

    <?php
        if(isset($_POST['updateQuantity'])) {
        //Connect to DB
        $hostname = "******";
        $username = "******";
        $password = "******";
        $dbName = "******";
    
        $con = mysqli_connect($hostname, $username, $password, $dbName);
    
        //Get Value From User
        $itemQuantity = $_POST["itemQuantity"];
        $ID = $_POST["ID"];
    
        //Query to Update Data
        $query = "UPDATE `Items` SET `itemQuantity`='$itemQuantity' WHERE ID='$ID'";
        $result = mysqli_query($con, $query);
    
        //Check if Query Was Successful
        if($result) {
            echo "<p style=font-family:'Roboto Condensed', sans-serif>Item quantity has been updated</p>";
        } else {;
            echo "<p style=font-family:'Roboto Condensed', sans-serif>Error updating the quantity of the item.</p>" . mysqli_error();
        }   
        //Disconnect From DB
        mysqli_close($con);
        }
        ?>
    <body>
    </body>
    </html>
    

0 个答案:

没有答案