I have used a submit button with an action key redirecting to a different page in other circumstances, but never with a loop involved while pulling from a database. How should I go about this? The first section is from the original page and the second is from the redirect page to store in a database.

<form action="insertorder.php" method="post">
<input type="submit" class="btn btn-dark" value="Submit">
<?php while ($row = $result->fetch_assoc()) :?>

<tr>
    <td name="Item_ID"><?=$row["item_id"]?></td>
    <td name="name"><?=$row["ITEM_NAME"]?></td>
    <td name="suggested_qty"><?=$row["suggested_qty"]?></td>
    <td  name="price" class="pricetd"><?=$row["Price_item"]?></td>
    <td>
    <input type="text" name="editedvalues[]" class="qtyinput" value="<?=$row["suggested_qty"]?>" />
    </td>
    <td><input name='result' class="resultinput" /></td>
</tr>

<?php endwhile?>

</table>
</form>

////////////////////////
$ItemID = mysqli_real_escape_string($conn,$_POST['Item_ID']);
$ItemName = mysqli_real_escape_string($conn,$_POST['name']);
$SuggQty = mysqli_real_escape_string($conn,$_POST['suggested_qty']);
$Price = mysqli_real_escape_string($conn,$_POST['price']);
$OrderQty = mysqli_real_escape_string($conn,$_POST['editedvalues[]']);
$TotalCost = mysqli_real_escape_string($conn,$_POST['result']);

$sql = "INSERT INTO ms_order (item_id, item_name, suggested_qty, price_item, order_ouantity, total) VALUES ('$ItemID', '$ItemName', '$SuggQty', '$Price', '$OrderQty', $TotalCost)";

if ($conn->query($sql) === TRUE) {
    echo "Submitted! Click here to return to Commodity Page:";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

0 Answers