0

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&uacute;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:

last two data

user3236149
  • 167
  • 4
  • 16
  • doe the number of fields here (cod, nombreProd, .......... , id_user, nombre, ref_compra) match the number of fields here $insertQuery[] = '(?, ?, ........ , ?, ?, ?)'; ? – dbinns66 Apr 21 '15 at 23:03
  • @dbinns66 yes all fields match, the only thing is with these three rows...because they 're only saved in the first insert...and with the others insert(per example 3,6,9) are empty – user3236149 Apr 21 '15 at 23:18
  • I doubt this is the problem, but your html inputs specify type twice, hence I'm wondering if they're actually posting the values properly. – Nicholas Byfleet Apr 21 '15 at 23:24
  • @NicholasByfleet all is ok with the data...I upload one pic to show the issue with these three rows...you can see it in the last two the first is all ok but the last are null – user3236149 Apr 21 '15 at 23:30
  • if you dump `$_POST` does it contain all the values you're expecting to enter? – pala_ Apr 22 '15 at 01:33
  • @pala_ yes all the values are in the dump – user3236149 Apr 22 '15 at 11:47
  • @pala_ The thing is, all the products are in the array the "n" times added in form, but this three inputs 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 – user3236149 Apr 22 '15 at 15:03

1 Answers1

0

With the help of @Dan08 I fix it, now I can save any row in multiple value form.

Here the Answer:

So you have a single userID you want to INSERT for each product record. I can probably provide a better answer if you post the output of print_r($_POST), but basically in your foreach $POST loop, keep the user ID static:

foreach ($_POST['cantidad'] as $i => $cantidad) {
  $insertQuery[] = '(?, ?, ?)';
  $insertData[] = $_POST['cantidad'][$i];
  $insertData[] = $_POST['nombreProd'][$i];
  $insertData[] = $_POST['Id']; //OR $insertData[] = $_POST['Id'][0], depending on $_POST array
            }

Here the link: answer for an static value's in multiple value form

Community
  • 1
  • 1
user3236149
  • 167
  • 4
  • 16