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 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义