PHP MySQL从数组键插入多维关联数组构建查询 [英] PHP MySQL insert multimensional associative array building query from array keys

查看:101
本文介绍了PHP MySQL从数组键插入多维关联数组构建查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了两个链接,但是在$ fields和$ newdata中没有得到预期的变量内容

I have tried both the links but I don't get the expected variables content in $fields and $newdata

**这个问题是在这里提出的一个新问题** PHP如何从关联数组中提取键名称和值以进行mysql查询(因为我是新手,所以做了一些工作以正确的方式提出问题)

** This question has developed in a new one here ** PHP how to extract keys names and values from associative array for mysql query (as explained I'm newbie and it took some work to get the question asked in the right way)

因此,我在这里打印一些var_dump并进行转换以恳请您的支持.

so I kindly print here some var_dump and cast to kindly ask your support.

为简单起见,在我的学习实验中,我正在使用仅包含5个字段的虚拟表,如您所见,它们是:selecteduser_iduser_nameuser_companyuser_email. 最终,我只插入了两行值.

For simplicity in my learning experiment, I'm working with a dummy table of just 5 fields, as you see they are: selected, user_id, user_name, user_company and user_email. Finally I have inserted just 2 rows of values.

使用此代码

$Arr = (array)$data;
print_r ( $Arr );

我可以看到此输出

Array ( 
[0] => Array ( [selected] => [user_id] => 3 [user_name] => nome3 [user_company] => azien3 [user_email] => email3 ) 
[1] => Array ( [selected] => 1 [user_id] => 6 [user_name] => nome6 [user_company] => azien6 [user_email] => email6 ) 
)

接下来,我尝试应用您链接的代码

next I try to apply the code of your links

24  $fields = implode(",", array_keys($Arr));
25  $newdata = "'" . implode("','", $Arr) . "'";
26  
27  var_dump($fields);
28  echo "<br><br>";
29  var_dump($newdata);

但是在我的解释或代码中出了点问题,因为输出是

But something is wrong in my interpreteation or in my code , because the output is

Notice: Array to string conversion in D:\xampp\htdocs\ajax-json\post.php on line 25

Notice: Array to string conversion in D:\xampp\htdocs\ajax-json\post.php on line 25
string(3) "0,1"

string(15) "'Array','Array'"

请问出什么问题了吗? 例如我的数组格式正确吗?

can you kindly point out what's wrong? e.g. is my array properly formed?

原始问题

我是新手,进一步阅读后我明白了这一点,希望现在我的问题可能会更简单.谢谢您的提示.

I'm newbie and after further readings I got the point and hopefully now my question could result more simple. Thank you for any hint.

我有一个二维数组,因为它的记录来自html表.

I have a bidimensional array since its records are coming from an html table.

在html端生成的集合会生成一个关联数组,因此每一行都类似于以下示例,在该示例中,您还可以看到,键很多

The collection made on the html side generates an associative array so each row is like in the following example where, also, you can see, the keys are many

[item] => Array ( 
    [0] => Array ( 
        [unit] => "car"
        [qty] => 3 
        [description] => "Description 1"
        ....
        [key47] => "thin"
    ) 
    [1] => Array ( 
        [unit] => "bike"
        [qty] => 74 
        [description] => "Description 2"
       ....
        [key47] => "large"
    ) 
)

我要在其中插入项目的mysql表名称为items_table

The mysql table name where I want to insert the items is named items_table

单位",数量"和描述"以及所有其余键(共47个)与items_table列的名称完全相同

"unit" "qty" and "description" and all the remaining keys (total 47) are exactly the same names of the items_table columns

在php端,我想使用PDO进行某种自动查询生成,该查询将从内部数组键名称中选择列名称,并将相应的键值插入正确的列中.

When on the php side I'd like to have a sort of automatic query generation maybe with PDO that would pick the columns names from the inner array keys names and will insert the corresponding key value in the correct column.

目标是避免创建如此笨拙且非常长的查询.

The goal is to avoid creating such a awkward and extremely long query.

首选方法是使用PDO,避免绑定47个键.

Preferred method would be with PDO, avoid binding 47 keys.

这种自动操作也很有用,因为将来可能会添加和/或删除和/或重命名mysql表中的列,因此我想只考虑正确地编辑html表而忽略了查询(关于那数组已经使用某些javascript自动生成了)

This sort of automatism is also useful because in the future columns in the mysql table may being added and or removed and or renamed, so I'd like to forget about the query thinking only about properly editing the html table (on that side the array is already automatically generated with some javascript)

您认为可以这样做吗?

Do you think is possible to do this?

如果可以,怎么办?

这还需要一些foreach循环吗?

Does this still need some foreach loop?

感谢您的帮助

推荐答案

例如:

core_table
id* unit quantity description
 1  car         3 Description 1
 2  bike       74 Description 2

adjunct_table
core_id* meta_key* meta_value
      1        47  Description of key n.47 first row
      2        47  Description of key n.47 second row

* = (component of) PRIMARY KEY

这篇关于PHP MySQL从数组键插入多维关联数组构建查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆