doushu8260
2016-05-26 06:00
浏览 38
已采纳

PHP:将动态生成的文本字段值存储在数据库中

I have one array contains the result set from dynamically generated text box values.

In the below example I created three dynamically generated rows and each row contains 6 text field. For differentiate each row name i added the row id as the last word of name. Example ClaimExecutionCountry1 means ClaimExecutionCountry as the name and 1 is the row id.

Array
(
[0] => ClaimExecutionCountry1=10
[1] => activitystartdate1=05-27-2016
[2] => activityenddate1=06-24-2016
[3] => CLCode1=CLC1
[4] => SCSCode1=SCS1
[5] => fileName1=calc2.png
[6] => ClaimExecutionCountry2=53
[7] => activitystartdate2=05-27-2016
[8] => activityenddate2=05-28-2016
[9] => CLCode2=
[10] => SCSCode2=
[11] => fileName2=gh.png
[12] => ClaimExecutionCountry3=82
[13] => activitystartdate3=05-26-2016
[14] => activityenddate3=07-28-2016
[15] => CLCode3=
[16] => SCSCode3=SCS5
[17] => fileName3=preview1.png
)

I am facing one issue for storing these values in Database. In my database structure is below

Id  |   ClaimExecutionCountry  |   activitystartdate  |  activityenddate  | CLCode  | SCSCode  |  fileName

I need to store after = symbol values in this table.

after insert the table, the result would be

 Id  |   ClaimExecutionCountry  |   activitystartdate  |  activityenddate  | CLCode  | SCSCode  |  fileName
------------------------------------------------------------------------------------------------------------
 1   |         10               |      05-27-2016      |     06-24-2016    |   CLC1  |  SCS1    |  calc2.png  
 2   |         53               |      05-27-2016      |     05-28-2016    |   null  |  null    |  gh.png   
 3   |         82               |      05-26-2016      |     07-28-2016    |   null  |  SCS5    |  preview1.png 

So Anyone please help me to store the array values in database using above format. I think you understood my problem. I am using PHP,codignator and MySql as database. Thanks in advance

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpymrcl269187540 2016-05-26 07:55
    已采纳

    Please try code below:

    $_array=Array(
            [0] => ClaimExecutionCountry1=10
            [1] => activitystartdate1=05-27-2016
            [2] => activityenddate1=06-24-2016
            [3] => CLCode1=CLC1
            [4] => SCSCode1=SCS1
            [5] => fileName1=calc2.png
            [6] => ClaimExecutionCountry2=53
            [7] => activitystartdate2=05-27-2016
            [8] => activityenddate2=05-28-2016
            [9] => CLCode2=
            [10] => SCSCode2=
            [11] => fileName2=gh.png
            [12] => ClaimExecutionCountry3=82
            [13] => activitystartdate3=05-26-2016
            [14] => activityenddate3=07-28-2016
            [15] => CLCode3=
            [16] => SCSCode3=SCS5
            [17] => fileName3=preview1.png
            )
            foreach($_array as $val){
                $a = explode("=",$val);
                $field = $a[0];
                $ans=$a[1];
                $matches = array();
                if (preg_match('#(\d+)$#', $field, $matches)) {
    
                    $rowNum=$matches[1];
                }
                $fieldName = str_replace($rowNum,"",$field);
                /*Now you have number of row , $fieldName , $rowNum and $ans so we can execute SQl statement inside forEach*/
            }
    

    Hope it will help you.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题