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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?