dongsha2792 2016-07-28 00:57
浏览 146
已采纳

如何使用PHP在单个foreach循环中将三个值添加到数据库中?

I have an input form with two values as a key, constructed this way:

<input name="verificarPago['.$x['pagoID'].'-'.$x['userID'].']" type="text">

So, the HTML would look like, for example, like this:

<input name="verificarPago[4-10]" type="text">

Besides that I have the value that the user can type, for example "Juan".

The inputs are created programmatically, and I want to save them each three values (in the example: 4, 10, Juan) into a database.

So, when the user submits the form, I thought on using foreach in order to access the elements in that array, but my problem is: How may I explode the keys to access the two values separately?

I've tried this (I'm not taking into account security now, I'm trying to prove the concept):

              $verificar = $_POST['verificarPago'];
              foreach ($verificar as $pago => $curso) {
//I'm pretty much stuck here. 
                  $verificar= array_flip(explode("-", $verificar));

//I want to insert the three items into the database
                  $verificarPago = "INSERT pagosVerificados SET userID = '$usuario', pagoID = '$pago', cursoID = '$curso'";
                  $cargarPago = mysqli_query($conectar, $verificarPago);

              }

I've tried adding to the form another input type, so the values wouldn't be together, but that way I should do a foreach inside a foreach and that renders duplicated results (and with errors as well).

  • 写回答

2条回答 默认 最新

  • dtvpe4837413 2016-07-28 02:10
    关注

    Without addressing any architectural issues, adding error-checking, or addressing security problems, here's how to do what you're asking for

    $verificar = $_POST['verificarPago'];
    foreach ($verificar as $ids => $text) {
        list($pagoID, $cursoID) = explode("-", $ids);
    
        $sql = "INSERT INTO pagosVerificados (userID, pagoID, cursoID, someVarcharField)
                          VALUES ($usuario, $pagoID, $cursoID, '$text');";
        $cargarPago = mysqli_query($conectar, $sql);
    
    }
    

    This is a tad inefficient though - executing an insert for every iteration. Since mysql supports bulk inserts, you can modify the generation of the query to leverage that.

    $verificar = $_POST['verificarPago'];
    $rows = array();
    
    foreach ($verificar as $ids => $text) {
        list($pagoID, $cursoID) = explode("-", $ids);
        $rows[] = "($usuario, $pagoID, $cursoID, '$text')";
    }
    
    $sql = "INSERT INTO pagosVerificados (userID, pagoID, cursoID, someVarcharField) VALUES "
                   . implode(',', $rows)
                   . ";";
    $cargarPago = mysqli_query($conectar, $sql);
    

    Now it's just one round-trip to the database

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 (求远程解决)深信服vpn-2050这台设备如何配置才能成功联网?