donoworuq450547191 2013-06-13 12:34
浏览 16
已采纳

PHP从数组创建查询,无法在foreach语句中挑选出两个索引

I am trying to create a query from a form that passes ID and Details fields, the post array looks like this:

array(2) { 
["ID"]=> array(9) 
    { 
    [0]=> string(3) "10a" 
    [1]=> string(3) "10b" 
    [2]=> string(3) "10c" 
    [3]=> string(3) "10d" 
    [4]=> string(3) "10e" 
    [5]=> string(3) "10f"  
    } 
["Details"]=> array(9) 
    { 
    [0]=> string(19) "This is textfield 1" 
    [1]=> string(17) "This is textfield 2" 
    [2]=> string(0) "" 
    [3]=> string(0) "" 
    [4]=> string(0) "" 
    [5]=> string(0) ""
    } 
}

The ID is hardcoded into the page and always passed, the details can be filled out by the user as needed.

 if ($_POST['OthProb']['Details'] != '') {
        // Catch issue text and assign to variable
        foreach ( $_POST['OthProb']['Details'] as $key => $value) {
            $id = $_POST['OthProb']['ID']->$key;
            $dets = $value;
        // Focus on fields with values and join the array values together in a query string
        if ($dets != ''){
        $string = "INSERT into $table (AccountID, OtherID, ID, Details) VALUES ($_POST[AccountID], $_POST[OtherID], '".$id.", '".$dets."')";
            $sql = $DBH->prepare($string);
            // $sql->execute();
            // var_dump ($id);                
   }   
   }

}

creates the following

 "INSERT into tbl_problems (AccountID, OtherID, ID, Details) VALUES (80, 80, '10f, 'This is Title issue')"

The 'Details' are outputting correctly, but its cycling all the way through the ID's to the last one, is that because I'm nesting a foreach? What would be the best way to structure the loop?

  • 写回答

1条回答 默认 最新

  • dspv70887 2013-06-13 12:38
    关注

    Try to replace this line:

    $id = $_POST['OthProb']['ID']->$key;
    

    by

    $id = $_POST['OthProb']['ID'][$key];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料