I have been trying to insert more than one records into my database table, but all i get is zeros(0) inserted into the fields. Below is what I tried. I tried using the foreach loop for insertion, but its not working for me. There any easy way to do it.Thanks
The html
<form method="post" action="data_handlers/purchase_entry_process.php">
<table>
<thead>
<tr class="titlerow">
<th>Item name</th>
<th>Quantity</th>
<th>Purchase Price</th>
<th>Amount</th>
<th>Description</th>
</tr>
</thead>
<tr>
<td class="col-md-3"><select name="item_code[]">
<option></option>
<?php
$tempholder = array();
$query = $link->query("SELECT * FROM items");
$nr = mysqli_num_rows($query);
for($i=0; $i<$nr; $i++) {
$row = mysqli_fetch_array($query);
if(!in_array($row['item_code'],$tempholder))
{
echo"<option value=\"{$row['item_code']}\" >{$row['item_name']}</option>";
}
}
?>
</select></td>
<td><input type="text" name="qty"></td>
td><input type="text" name="purchase_price"></td>
<td><input type="text" name="sub_total"></td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td class="col-md-3"><select name="item_code[]">
<option></option>
<?php
$tempholder = array();
$query = $link->query("SELECT * FROM items");
$nr = mysqli_num_rows($query);
for($i=0; $i<$nr; $i++) {
$row = mysqli_fetch_array($query);
if(!in_array($row['item_code'],$tempholder))
{
echo"<option value=\"{$row['item_code']}\" >{$row['item_name']}</option>";
}
}
?>
</select></td>
<td><input type="text" name="qty"></td>
td><input type="text" name="purchase_price"></td>
<td><input type="text" name="sub_total"></td>
<td><input type="text" name="description"></td>
</tr>
</table>
</form>
php
$item_code = $_POST['item_code'];
$qty = $_POST['qty'];
$pur_price = $_POST['purchase_price'];
$sub_total = $_POST['sub_total'];
$desc = $_POST['description'];
foreach($item_code as $item_codes);
$insert_inventory = $link->query("INSERT INTO inventory VALUES ('NULL','$item_codes','$qty','$pur_price','$qty','$desc','$sub_total')");
array(5) { ["item_code"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } ["qty"]=> string(1) "2" ["purchase_price"]=> string(2) "22" ["sub_total"]=> string(3) "222" ["description"]=> string(4) "2222" }