doumizhi0809 2015-04-21 22:43
浏览 51
已采纳

如何以多行格式保存用户名和user_id

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

  • 写回答

1条回答 默认 最新

  • dsdqpdjpq16640651 2015-05-12 23:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记