0

I have the first page that displays current inventory. Works fine. I have the second page that displays items that match the SKU entered on the first page. If I select the first one, values for dateSold, retail (actual Retail), and tax don't carry to the update page. The SKU and the ID# do. If I select the last one for that SKU, everything works fine. If there is only one item matching the SKU, it works fine. I am at a loss as to why?

I realized why the last or if only one item works. How can it make it so if I use radio buttons it take the one marked, or even better if I use checkboxes have it post all checked?

<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>

Page two where the problem starts:

        
                <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.sstatic.net/ED8t4.png)

Code from Update Page:

<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>

I don't understand why checking the radio button for the first item (or any other except the last) doesn't carry over, while checking the last item does. Or if it is only one item it works fine. Is there a check that I need to do to get only the Radio button that is checked to carry over? or should it be a checkbox (which would be preferred so I could select multiple items.)

2
  • Your code is vulnerable to SQL injection attacks. Instead of building queries with string concatenation, always use prepared statements with bound parameters. See this page and this post for some good examples. Commented Jul 23, 2023 at 18:15
  • Since this will be on a standalone system without internet access or anyone but the store owner having access that is immaterial to the function. I am well aware of SQL injection attacks and how to protect against them using prepared statements. If I choose I can alter the SQL and PHP to reflect that. But Function is more important currently. The rest can come later.
    – smaschek
    Commented Jul 23, 2023 at 19:10

1 Answer 1

0

In your second page, where users can make updates you have form. Within this form, you are dynamically generating table rows with radio buttons for each record. The problem is when you have multiple records for a single SKU, as each of those rows is generated with it's own form elements, but all are enclosed within the same form. If the user selects the radio button for the last record and submits, then all the form elements of the previous record (which have empty values) will overwrite the values of the selected records.

To fix this you could separate forms, instead of having one big form for all records, you could create separate form for each record OR you can use Javascript to ensure that only the selecetd data is sent.

2
  • I have been trying to figure out the JS code to only send data of a checked box, but haven't been successful with that yet either. Admittedly, I am pretty weak with anything other than basic JS.
    – smaschek
    Commented Jul 23, 2023 at 18:42
  • 1
    The Answer from MorganFreeFarm was the clue to resolving this without a whole lot of complications. I created my "Form" inside of the "While" statement so that each entry in the database dynamically created a form. Made the "Submit" button the link and Boom dynamically creates the page with the correct entry and I can update no problem.
    – smaschek
    Commented Jul 27, 2023 at 2:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.