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 超声波 合成孔径 扩散角
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥15 Delphi image图片缩放问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥15 初学者用plt报错,求解答
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退