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.stack.imgur.com/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.)