Я пытаюсь вставить несколько записей в мою таблицу базы данных, но все, что я получаю, это нули (0), вставленные в поля. Ниже я попытался. Я попытался использовать петлю foreach для вставки, но это не работает для меня. Там любой простой способ сделать it.thanksВставка нескольких записей в mysql с использованием php
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')");
Вы пытаетесь вставить массив как $ item_code. Количество, цена, общее количество и описание содержат только информацию из второй строки полей ввода. Ниже приведена справка запроса POST из вашей формы, первая строка получена (1) в качестве входных данных, вторая строка получена двумя (2). '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"} ' – Baldvin
@Baldvin, пожалуйста, помогите мне. как его исправить – ASM