MYSQL&;PHP将数据发送到数据库并将数组包含到行中

MYSQL&;PHP将数据发送到数据库并将数组包含到行中,php,sql,arrays,Php,Sql,Arrays,你好,我希望你今天很好 我有一个表单,它使用$\u会话存储表单末尾的数据,我要求表单提交到数据库 在我的表单中,有一个选项允许用户根据其子项添加新的输入 例如,如果他们的用户有一个子用户,他们按下一个按钮,jQuery添加一个输入字段,然后该输入将数据输出到会话中 $_会议[“儿童”] 这是数组数据(其中包含一些示例) 我的问题是,我现在正在将数据提交到数据库,我可以在一个SQL查询中将表单的所有其他实例(例如“first_name”“last_name”)发送到数据库,而不会出现任何问题: $

你好,我希望你今天很好

我有一个表单,它使用$\u会话存储表单末尾的数据,我要求表单提交到数据库

在我的表单中,有一个选项允许用户根据其子项添加新的输入

例如,如果他们的用户有一个子用户,他们按下一个按钮,jQuery添加一个输入字段,然后该输入将数据输出到会话中

$_会议[“儿童”]

这是数组数据(其中包含一些示例)

我的问题是,我现在正在将数据提交到数据库,我可以在一个SQL查询中将表单的所有其他实例(例如“first_name”“last_name”)发送到数据库,而不会出现任何问题:

$sql  = "INSERT INTO wills(";
$sql .= implode(", ",$in_fields);
$sql .= ") VALUES (";
$sql .= implode(", ",$in_values);
$sql .= ")";
首先,上面的SQL insert查询不会将儿童添加到数据库中,因为它会获取值“array”

我计划将数据以序列化格式存储到children字段中,以便查询

我在尝试将数组插入children字段时遇到了问题,但我成功地将它作为虚拟测试在没有任何变量的情况下运行

$sql  = "INSERT INTO wills(children) VALUES (bob john simon)";
上面的问题还在于,它将其添加到新的数据库行,而不是同一行

以下是完整的代码:

// sql fields and values
$in_fields = array();
$in_values = array();
$in_children = serialize($_SESSION['children']);

foreach($_SESSION as $key => $value) {
    if(!empty($value)) {
        $value = sql_escape($value);
        $key = explode("#",$key);
            $in_fields[] = "`{$key[0]}`";
            $in_values[] = "'{$value}'";
    }
}


if(!empty($in_fields)) {




    $sql  = "INSERT INTO wills(";
    $sql .= implode(", ",$in_fields);
    $sql .= ") VALUES (";
    $sql .= implode(", ",$in_values);
    $sql .= ")";



    executeSql($sql);

}
打印我的变量

$in_字段

Array ( [0] => `selection` [1] => `first_name` [2] => `middle_name` [3] => `last_name` [4] => `spouse_name` [5] => `street` [6] => `city` [7] => `state` [8] => `zipcode` [9] => `email` [10] => `phone` [11] => `cell` [12] => `children_spouse` [13] => `deceased_children_option` [14] => `children_deceased` [15] => `children_under_18` [16] => `1st_guardian` [17] => `2nd_guardian` [18] => `age` [19] => `disabled_children` [20] => `children_disabled` [21] => `children` [22] => `trustee_1` [23] => `trustee_2` [24] => `trustee_children` [25] => `executor_1st` [26] => `executor_2nd` [27] => `spouse_executor_1st` [28] => `split_trust` [29] => `child_share` [30] => `grandchild_share` [31] => `child_share_split` [32] => `distribute_outside` [33] => `share_outside` [34] => `spouse_how_share` [35] => `child_spouse_share` [36] => `spouse_distribute_outside` [37] => `poa_name_1` [38] => `poa_relationship_1` [39] => `poa_telephone_1` [40] => `poa_name_2` [41] => `poa_relationship_2` [42] => `poa_telephone_2` [43] => `spouse_poa_name_1` [44] => `spouse_poa_relationship_1` [45] => `spouse_poa_telephone_1` [46] => `spouse_poa_name_2` [47] => `spouse_poa_relationship_2` [48] => `spouse_poa_telephone_2` [49] => `hc_poa_name_1` [50] => `hc_poa_relationship_1` [51] => `hc_poa_telephone_1` [52] => `hc_poa_name_2` [53] => `hc_poa_relationship_2` [54] => `hc_poa_telephone_2` [55] => `spouse_hc_poa_name_1` [56] => `spouse_hc_poa_relationship_1` [57] => `spouse_hc_poa_telephone_1` [58] => `spouse_hc_poa_name_2` [59] => `spouse_hc_poa_relationship_2` [60] => `spouse_hc_poa_telephone_2` [61] => `physician` [62] => `spouse_physician` [63] => `deeds` [64] => `deed` ) 
$in_值

Array ( [0] => '2nd_Marriage' [1] => 'First nAME' [2] => 'Middle Name' [3] => 'Last Name' [4] => 'Spouse Full Name' [5] => 'Street Address' [6] => 'City' [7] => '2' [8] => 'Zipcode' [9] => 'email Address' [10] => 'phone number' [11] => 'cell number' [12] => '' [13] => 'yes' [14] => '' [15] => 'yes' [16] => 'Guardian 1' [17] => 'Guardian 2' [18] => '20' [19] => 'yes' [20] => '' [21] => '' [22] => 'Sucessor Trustee 1' [23] => 'Sucessor Trustee 2' [24] => 'Children Sucessor Trustee' [25] => 'First Executor' [26] => 'Second Executor' [27] => 'Spouse\'s Executor' [28] => 'yes' [29] => 'yes' [30] => 'yes' [31] => 'yes' [32] => 'yes' [33] => 'Estate Distrubution' [34] => 'Spouse\'s Estate Distribution' [35] => 'no' [36] => 'no' [37] => 'POA 1' [38] => 'Relationship poa 1' [39] => 'telephone poa 1' [40] => 'POA 2' [41] => 'Relationship poa 2' [42] => 'telephone poa 2' [43] => 'spouse poa 1' [44] => 'spouse Relationship poa 1' [45] => 'spouse telephone poa 1' [46] => 'spouse poa 2' [47] => 'spouse Relationship poa 2' [48] => 'spouse telephone poa 2' [49] => 'poa health 1' [50] => 'poa health relationship 1' [51] => 'poa health telphone 1' [52] => 'poa health 2' [53] => 'poa health relationship 2' [54] => 'poa health telphone 2' [55] => 'spouse poa health 1' [56] => 'spouse poa health relationship 1' [57] => 'spouse poa health telphone 1' [58] => 'spouse poa health 2' [59] => 'spouse poa health relationship 2' [60] => 'spouse poa health telphone 2' [61] => 'yes' [62] => 'yes' [63] => 'yes' [64] => '5' ) 
$in_儿童

Array ( [0] => Natural Chlid 1 [1] => Natural Chlid 2 [2] => Natural Chlid 3 ) 

不确定您的问题是什么,但请注意,插入的字符串值必须用引号括起来:

INSERT INTO myTable (field1, field2) VALUES ('value1', 'value2')

使用单引号或双引号并不重要,但在您的情况下,最好使用单引号,因为php字符串使用双引号。

我的问题是,我无法获取子会话的数据(这是一个数组)要输入到我试图提交到的行中,您为什么要在foreach中迭代
$\u SESSION
?难道您不想在会话['children']中使用所有键/值吗?
INSERT INTO myTable (field1, field2) VALUES ('value1', 'value2')