我正在为我当地的游戏店开发一个库存程序(基于网络)。尝试创建更新页面。价值不会结转

I am working on an inventory program (web based) for my local gameshop. Trying to create an update page. Values are not carrying over

提问人:smaschek 提问时间:7/24/2023 最后编辑:smaschek 更新时间:7/24/2023 访问量:31

问:

我有显示当前库存的第一页。工作正常。 我有第二页,其中显示与第一页上输入的 SKU 匹配的商品。 如果我选择第一个,则 dateSold、retail(实际零售)和 tax 的值不会带到更新页面。SKU 和 ID# 确实如此。如果我为该 SKU 选择最后一个,则一切正常。 如果只有一个项目与 SKU 匹配,则工作正常。我不知所措,为什么?

我意识到为什么最后一个或只有一个项目有效。如果我使用单选按钮,它如何做到这一点,它会选择标记的按钮,或者如果我使用复选框,则将其全部选中,甚至更好?

<fieldset>
                    <h2>Update Inventory</h2>
                    <h5><span style="color:black">Please complete the form below. All fields are Required</span> * </span></h5>
             
                    <form method="POST" class="updateInventory" name="updateInventory" action="../updates/changeUpdateInventory.php" id="updateInventory" >
                        <?php
                             $SKUErr = "";
                        
                        ?>  
                            <table>         
                        <fieldset id="Inventory" >  
                            <tr><td>SKU Code: </td><td><input type="textbox" id="SKU" name="SKU" value="" size="50" autofocus></input></td></tr>                
                            </table>
                            
                            <!-- <input type="submit" value="Submit"> -->
                             <button type="submit" value="Submit" class="submit"  onClick="return checkForm(this.parentNode)" >Submit</button> 
                            <br>
                    </form>
                    <br>
                    </fieldset>
                </fieldset>
                
                <div>
                <?php
                require_once '../includes/main2.php';
                
                try {
                    $pdo = new PDO('mysql:host=' . db_host . ';dbname=' . db_name . ';charset-' . db_charset, db_user, db_pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
                    $skillquery = "SELECT * FROM `inventory` WHERE `dateSold` <= '0000-00-00' ORDER BY `date`, `category`, `itemName`";
                    $q = $pdo->query($skillquery);
                    $q->setFetchMode(PDO::FETCH_ASSOC);
                } catch (PDOException $e) {
                        die ("Could not connect to the database $db_name :" . $e->getMessage());
                }
        ?>
                
                <table style="width:100%"> 
                <tbody>
                    <tr>
                        <th style="width:15%">SKU: </th>
                        <th style="text-align:center; width:10%">Consignee</th>
                        <th style="text-align:center; width:20%">Item Name</th>
                        <th style="text-align:center; width:10%">Category</th>
                        <th style="text-align:center; width:20%">Description</th>
                        <th style="text-align:center; width:5%">Retail</th>
                        <th style="text-align:center; width:5%">Wholesale</th>
                        <th style="text-align:center; width:15%">Date</th>
                    </tr>
                    <?php
                        $bg=1;
                        while ($row = $q->fetch()):
                        if($bg == 1){
                            echo "<tr class='odd'>";
                            $bg=2;
                        }else{
                            echo "<tr class='even'>";
                            $bg=1;
                        }
                    ?>  
                    
                    
                        <td><?php echo ($row['SKU']); ?></td>
                        <td><?php echo ($row['consignee']); ?></td>
                        <td style="text-align:center"><?php echo ($row['itemName']); ?></td>
                        <td style="text-align:center"><?php echo ($row['category']); ?></td>
                        <td style="text-align:center"><?php echo ($row['description']); ?></td>
                        <td style="text-align:center"><?php echo ($row['retail']); ?></td>
                        <td style="text-align:center"><?php echo ($row['wholesale']); ?></td>
                        <td style="text-align:center"><?php echo ($row['date']); ?></td>
                    </tr>
                    
                    <?php endwhile; ?>
                    </tbody>
                </table>
                    
                </div>

问题开始的第二页:

        
                <fieldset>
                    <h2>Add Inventory</h2>
                    <h5><span style="color:black">Please complete the form below. All fields are Required</span> * </span></h5>
             
        <form method="POST" class="changeUpdateInventory" name="changeUpdateInventory" action="../updates/processUpdateInventory.php" id="changeUpdateInventory" >
            <?php
                 $idErr = $SKUErr = $dateSoldErr = $consigneeErr = $itemNameErr = $categoryErr = $descriptionErr = $retailErr = $wholesaleErr = $taxErr = $dateErr = "";            
            ?>  
                <table>         
            <fieldset id="Inventory" >  
                <tr>                
                <?php
                if (empty($_POST['SKU']) )
                {
                echo "<p>You must enter all requested information!<a href='../updates/updateInventory.php'><button type='submit' name='return'>Return to Form</button></a></p>";
                }
                else 
                {
                    $TableName = 'Inventory';
                    $SKU = stripslashes($_POST['SKU']);
                    /*
                    var_dump ($SKU);
                    echo $SKU;*/
        
                //Get information from db where SKU
                require_once '../includes/main2.php';
                
                try {
                    $pdo = new PDO('mysql:host=' . db_host . ';dbname=' . db_name . ';charset-' . db_charset, db_user, db_pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
                    $skillquery = "SELECT * FROM `inventory` WHERE `SKU` = '$SKU' AND `dateSold` <= '0000-00-00' ORDER BY `date`, `category`, `itemName`";
                    $q = $pdo->query($skillquery);
                    $q->setFetchMode(PDO::FETCH_ASSOC);
                } catch (PDOException $e) {
                        die ("Could not connect to the database $db_name :" . $e->getMessage());
                }
                ?>
                
                <table style="width:100%"> 
                <tbody>
                    <tr>                    
                        <th style="width:5%">Db ID: </th>
                        <th style="width:10%">SKU: </th>
                        <th style="width:10%">Selling Date: </th>
                        <th style="width:10%">Consignee</th>
                        <th style="width:10%">Item Name</th>
                        <th style="width:10%">Category</th>
                        <th style="width:5%">Retail</th>
                        <th style="width:10%">Actual Retail</th>
                        <th style=" width:5%">Wholesale</th>
                        <th style="text-align:center; width:5%">Tax</th>
                        <th style="width:10%">Date</th>
                    </tr>
                    <?php
                        $bg=1;
                        while ($row = $q->fetch()):
                        if($bg == 1){
                            echo "<tr class='odd'>";
                            $bg=2;
                        }else{
                            echo "<tr class='even'>";
                            $bg=1;
                        }
                    ?>  
                    
                    <!-- Check the radio to update this item -->    
                        <td style="text-align:center"><input type="radio" id="id" name="id" value="<?php echo ($row['id']); ?>"></input></td>
                    <!-- displays SKU left as input to ensure that it carries over in the Submit--> 
                        <td><input type="text" size="10" id="SKU" name="SKU" value="<?php echo ($row['SKU']); ?>"></input></td>
                    <!-- Enter date product was sold -->    
                        <td><input type="text" size="10" id="dateSold" name="dateSold" placeholder="Date sold" value=""></input></td>
                    <!-- displays Consignee's name, Item Name, and Category, has Item Description hidden only there for tracking and future needs -->   
                        <td><?php echo ($row['consignee']); ?></td>
                        <td><?php echo ($row['itemName']); ?></td>
                        <td><?php echo ($row['category']); ?></td>
                        <h2 class="hidden"><?php echo ($row['description']); ?></h2>
                    <!-- displays the current price of the item in the inventory -->    
                        <td>$<?php echo ($row['retail']); ?></td>
                    <!-- Should allow the retail price to be entered and updated when submitted --> 
                        <td><input type="text" id="retail" name="retail" value="" size="10" placeholder="Actual Price"> </input></td>
                    <!-- displays wholesale price of item -->   
                        <td style="text-align:center;">$<?php echo ($row['wholesale']); ?></td>
                    <!-- should allow the amount of tax collected to be entered and submitted -->   
                        <td><input type="text" size="5" id="tax" name="tax" value=""placeholder="Taxes"> </input></td>
                    <!-- displays the date that the item was added to inventory --> 
                        <td><?php echo ($row['date']); ?></td>
                        
                    </tr>
                    
                <?php endwhile; }?>
                    </tbody>
                </table>
                
                <!-- Submit -->
                 <button type="submit" value="Submit" class="submit" onClick="return checkForm(this.parentNode)" >Submit</button> 
                <br>
            </form>
            <br>
            </fieldset>
    </fieldset>

What I get: [enter image description here](https://i.stack.imgur.com/ED8t4.png)

更新页面中的代码:

<div>
            <?php
            if (empty($_POST['SKU']) )
                {
                    echo "<p>You must enter all requested information!<a href='../updates/changeUpdateInventory.php'><button type='submit' name='return'>Return to Form</button></a></p>";
                }
                    else 
                {
            $TableName = 'Inventory';           
            $id = stripslashes($_POST['id']);           
            $SKU = stripslashes($_POST['SKU']);
            $dateSold = stripslashes($_POST['dateSold']);           
            $retail = stripslashes($_POST['retail']);           
            $tax = stripslashes($_POST['tax']);
            

            /**/
            var_dump ($id);
            var_dump ($SKU);
            var_dump ($dateSold);           
            var_dump ($retail);         
            var_dump ($tax);
            
            
            
     // Insert the new event into the database  
            require_once '../includes/main2.php';
        try {
            $conn = new PDO('mysql:host=' . db_host . ';dbname=' . db_name . ';charset-' . db_charset, db_user, db_pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            // consultation:    
            
            $statement = "UPDATE `Inventory` SET `dateSold`='$dateSold', `retail`='$retail', `tax`= '$tax' WHERE `id` = '$id' AND `SKU` = '$SKU'"; 

            
            $conn->exec($statement);
            
            echo "New Inventory Update Entered.";
            
                } catch (PDOException $e) {
                    echo $statement . "<br>" . $e->getMessage ();
                }
       
        } //header('Location: ./register_success.php');
    
?>

</div>

我不明白为什么检查第一项(或除最后一项以外的任何其他项目)的单选按钮不会结转,而检查最后一项会结转。或者,如果它只是一个项目,它可以正常工作。我是否需要进行检查才能仅选中要结转的单选按钮?或者它应该是一个复选框(这是首选,这样我就可以选择多个项目。

php html sql 输入

评论

0赞 Alex Howansky 7/24/2023
您的代码容易受到 SQL 注入攻击。不要使用字符串串联来生成查询,而是始终使用带有绑定参数的预准备语句。有关一些很好的例子,请参阅此页面这篇文章
0赞 smaschek 7/24/2023
由于这将在一个没有互联网访问的独立系统上,或者除了店主之外的任何人都可以访问该功能,因此该功能无关紧要。我很清楚SQL注入攻击以及如何使用准备好的语句来防止它们。如果我选择,我可以更改 SQL 和 PHP 以反映这一点。但功能目前更为重要。其余的可以稍后再来。

答:

0赞 MorganFreeFarm 7/24/2023 #1

在您的第二页中,用户可以在其中进行更新,您有表单。在此窗体中,您将动态生成表行,其中包含每条记录的单选按钮。问题在于,当您对单个 SKU 有多条记录时,因为其中每一行都是使用其自己的表单元素生成的,但所有行都包含在同一个表单中。如果用户选择最后一条记录的单选按钮并提交,则上一条记录的所有表单元素(具有空值)将覆盖所选记录的值。

要解决此问题,您可以分离表单,而不是为所有记录使用一个大表单,您可以为每个记录创建单独的表单,或者您可以使用 Javascript 来确保仅发送 selecetd 数据。

评论

0赞 smaschek 7/24/2023
我一直在尝试找出仅发送复选框数据的 JS 代码,但也没有成功。诚然,除了基本的JS之外,我对其他任何事情都很弱。
1赞 smaschek 7/27/2023
MorganFreeFarm 的答案是解决这个问题的线索,没有太多的复杂性。我在“While”语句中创建了我的“Form”,以便数据库中的每个条目都动态地创建一个表单。将“提交”按钮作为链接,Boom 会动态创建具有正确条目的页面,我可以毫无问题地更新。