Reputation: 169
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')");
Upvotes: 0
Views: 56
Reputation: 280
To get the quantity, price, sub_total and description from all the fields, you should make them arrays like you did with item_code.
<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>
For processing the PHP and make it ready for the query you could do something akin to
<?php
// Prepare the query
$query = "INSERT INTO inventory VALUES ";
// Go through all the entries
foreach ($_POST['item_code'] as $key => $value) {
// If an item has been selected from the drop-down
// we process it for the query.
if (!empty($value) && $value != "") {
$query .= ($key > 0 ? ", " : "");
$query .= "(NULL, '" . $value . "', '" . $_POST['qty'][$key] . "', '" . $_POST['purchase_price'][$key] . "', '" . $_POST['sub_total'][$key] . "', '" . $_POST['description'][$key] . "')";
}
}
// Example output from $query
// INSERT INTO inventory VALUES (NULL, '1', '1', '11', '111', '1111'), (NULL, '2', '2', '22', '222', '2222')
?>
I would recommend throwing in some escaping of the variables as well, to prevent SQL injections.
Upvotes: 1