I am working with multiple row insert... all is ok with the multiple row all is saved into DB but now I added 3 rows more in the DB, two are for identify the user (username and user_id) and one to link all products created in the form for make the update (ref_compra)... so the problem is because these three rows are not saved the X times like the others items, just one time are saved.
The thing is, all the products are in the array the "n" times added in form, but this three inputs (ref_compra,id_user and nombre) because they are just one time in the form and are not added the "n" times like the other inputs when the button save is clicked, the array catch them just one time...do you know how can I add to save the "n" times in the DB like the another inputs
This part is in the beginning of the form, is the input of ref_compra name:
<div class="input-group">
<div class="input-group-addon">#</div>
<input type="text" class="form-control required" name="ref_compra[]" id="ref_compra" placeholder="número de factura de compra" required >
</div>
Here the second part of the form with the issues is in the final of the form:
<p>
<div class="btn-group btn-block">
<input type="hidden" name="action" value="create" />
<input type="hidden" id="nombre" name="nombre[]" type="text" value="<?php echo $userInfo['username']; ?>" />
<input type="hidden" id="id_user" name="id_user[]" type="text" value="<?php echo $userInfo['user_id']; ?>" />
<button type="button" class="btn btn-danger" onclick="goBack()"><i class="glyphicons glyphicons-left_arrow"></i> <?php echo ASLang::get('Back to the purchase list'); ?></button>
<input type="submit" id="submit" class="btn btn-info" value="<?php echo ASLang::get('Add purchase'); ?>" />
</div>
</p>
Here the insert code:
$conn->beginTransaction();
$sql = "INSERT INTO PRODUCTOS
(cod, nombreProd, .......... , id_user, nombre, ref_compra)
VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['cod'] as $i => $cod) {
$insertQuery[] = '(?, ?, ........ , ?, ?, ?)';
$insertData[] = $_POST['cod'][$i];
$insertData[] = $_POST['nombreProd'][$i];
..........
$insertData[] = $_POST['id_user'][$i]; // here the user_id
$insertData[] = $_POST['nombre'][$i]; // here the username
$insertData[] = $_POST['ref_compra'][$i]; // here the ref_compra
}
if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $conn->prepare($sql);
$stmt->execute($insertData);
}
$conn->commit();
I need to know why these three item are not saved like the other data which is saved "X" times (3,6,10 products)
Here the capture with the last two insert data...the first one is ok but the second is empty in those three row's: