I have a table and i'm trying to add multiple items from said table to a database. The data is being stored in a php array and then inserted into my database. I have multiple table rows that need to be inserted but only the last item in the table is the item that gets inserted into the database and I need everything from the table to go into the database.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "salestest";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sku = $_POST['txtSKULater'];
$productName = $_POST['txtProductNameLater'];
$quantity = $_POST['txtQuantityLater'];
$retailPrice = $_POST['txtRetailPriceLater'];
$listPrice = $_POST['txtListPrice555Later'];
$total = $_POST['txtTotalLater'];
$sql = "insert into orders(
sku, product_name, quantity, retail_price, list_price, total) values(
'$sku', '$productName', '$quantity',
'$retailPrice', '$listPrice', '$total')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
HTML:
<table id="table">
<tr>
<th><label>SKU:</label></th>
<th><label>Retail Price:</label></th>
<th><label>List Price</label></th>
<th><label>Product Name</label></th>
<th><label>Quantity</label></th>
</tr>
<tr>
<td><input type="text" name="txtSKU" value='123'></td>
<td><input type="text" name="txtRetail" value='11.75'></td>
<td><input type="text" name="txtList" value='4.50'></td>
<td><input type="text" name="txtProductName" value="product1"></td>
<td><input type="text" name="txtQuantity" value='1'></td>
</tr>
<tr>
<td><input type="text" name="txtSKU" value='456'></td>
<td><input type="text" name="txtRetail" value='12.25'></td>
<td><input type="text" name="txtList" value='9.40'></td>
<td><input type="text" name="txtProductName" value='product2'></td>
<td><input type="text" name="txtQuantity" value='1'></td>
</tr>
</table>
The vardump of the post array that is being added to the database looks similar this:
array(12) { ["txtCustomerNameLater"]=> string(13) "101 Auto Body" ["txtPhoneLater"]=> string(12)
"510-524-4857" ["txtEmailLater"]=> string(20) "henrykseng@yahoo.com" ["txtRopoLater"]=> string(5)
"test3" ["txtAddressLater"]=> string(55) "101 Auto Body 5327 Jacuzzi St Ste 3A Richmond, CA 94804"
["txtTotalLater"]=> string(4) "15.5" ["txtSKULater"]=> string(13) "8620844-0424R"
["txtRetailPriceLater"]=> string(5) "11.75" ["txtListPrice555Later"]=> string(4) "7.75"
["txtProductNameLater"]=> string(45) "Stone Guard Right Volvo XC 90 03-14 Rear Door"
["txtQuantityLater"]=> string(1) "1" ["submitOrder1"]=> string(0) "" } New record created successfully