在mysql数据库中保存数组 [英] Save array in mysql database

查看:193
本文介绍了在mysql数据库中保存数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在将总订单发送到 Paypal 之前保存额外信息.对于每个项目,我在我的 MySQL 数据库中创建了一个列,我想在其中存储它.现在我想将它保存为一个数组,稍后我可以读取它以创建一个 PHP 页面.额外的字段取自输入表单字段.

I want to save extra information before sending the total order to Paypal. For each item I have created a single column in my MySQL database where I want to store it. Now I was thinking to save it as an array which I can read later for creating a PHP page. Extra fields are taken from input form fields.

通过使用数组,我可以确保不会混淆信息吗?

By using an array can I be sure not to mixup information?

推荐答案

您可以使用 serialize/反序列化.使用该解决方案,其他编程语言无法轻松使用它们,因此您可以考虑使用 json_encode/json_decode 代替(它为您提供广泛支持的格式).避免为此使用implode/explode,因为您最终可能会遇到错误或安全漏洞.

You can store the array using serialize/unserialize. With that solution they cannot easily be used from other programming languages, so you may consider using json_encode/json_decode instead (which gives you a widely supported format). Avoid using implode/explode for this since you'll probably end up with bugs or security flaws.

请注意,这会使您的表未规范化,这可能是个坏主意,因为您无法轻松查询数据.因此,在继续之前仔细考虑这一点.您是否需要查询数据以进行统计或以其他方式查询?是否有其他原因需要对数据进行标准化?

Note that this makes your table non-normalized, which may be a bad idea since you cannot easily query the data. Therefore consider this carefully before going forward. May you need to query the data for statistics or otherwise? Are there other reasons to normalize the data?

另外,不要保存原始的 $_POST 数组.有人可以轻松制作自己的 Web 表单并将数据发布到您的站点,从而发送占用大量空间的非常大的表单.保存您想要的那些字段,并确保在保存之前验证数据(这样您就不会得到无效值).

Also, don't save the raw $_POST array. Someone can easily make their own web form and post data to your site, thereby sending a really large form which takes up lots of space. Save those fields you want and make sure to validate the data before saving it (so you won't get invalid values).

这篇关于在mysql数据库中保存数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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