插入阵列到MySQL数据库列 [英] Inserting an array into a mysql database column

查看:89
本文介绍了插入阵列到MySQL数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从多选择下拉插入值下的形式,以一个MySQL数据库列。例如:下拉会有那么选择时,形式发布,将数据插入一列在MySQL数据库的一个或更多的选择。我被困在如何插入数据。

I am trying to insert values from a multi select drop down in a form to a mysql db column. For example: The drop down would have one or more choices chosen then when the form is posted it would insert the data into one column in a mysql db. I'm stuck at how to insert the data.

推荐答案

如果你想在单行插入那么你可以使用破灭()来生成逗号分隔的数据,或者你可以做json_en code()和添加到您的柱。

If you want to insert in single row then you can use implode() to generate comma separated data, or you can do json_encode() and add to your colum.

假设你得到的数据作为

$data = array("one", "two", "tree");

// output one, two, three
$insert_data = implode(",", $data);


or  

$insert_data = json_encode($data);

这就是在单个列插入数据。当检索你可以做爆炸()或json_de code()来获取返回的数据,并可以在使用它们再次多选择。

Thats for inserting data in single column. While retrieving you can do explode() or json_decode() to get the return data and can use them in the multi-select again.

如果您想一行对每个项目然后就通过数组循环,并添加它们

If you want one row for each item then just loop through the array and add them

这篇关于插入阵列到MySQL数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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