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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64