PHP / MySQL:在数据库列中存储多个值 [英] PHP/MySQL: Storing Multiple Values in Database Column

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

问题描述

我有一个表格,可让您输入电话号码。您可以单击加号按钮并添加更多字段。我需要将所有这些值存储在数据库中,但只需要一列即可完成。

I have a form that allows you to input phone numbers. You can click a plus button and add more fields. I need to store all those values in the database but only have one column to do it.

电话号码也是一个类型下拉列表,您可以在其中选择以下内容:移动,家庭和办公。因此,我需要一种将每个字段的类型和数字配对在一起,然后将所有配对组合在一起的方法。然后,当我需要信息时,能够将其全部提取出来并将所有值放入数组中。

With the phone number is also a type dropdown where you can select things like: Mobile, Home, and Work. So I need a way to pair the type and number together for each field, then combined all the pairs together. Then later on when I need the information, to be able to pull it all out and place all values into an array. Almost like encrypting and decrypting.

我在想类似的东西:

type:number;type:number;type:number;type:number;type:number;type:number

能够使用爆炸和内爆进行加密和解密。

Being able to use explode and implode to encrypt and decrypt.

但这有多好?而且我还有一个搜索功能,可以搜索电话号码。我想我的方法对搜索功能来说并不容易。

But how good is this? And I also have a search feature which searches for phone numbers. I imagine my way can't be easy for a search function to look through.

推荐答案

我会说这是个坏主意。如果您打算使用有限的数字类型,例如家庭,办公室,移动电话等,建议您为每个数字添加列。否则,请创建一个单独的数据库表 phone_numbers,并将其与您的客户ID或类似名称相关联,并添加任意数量的号码。搜索将更快,更容易实现。

I would say its a bad idea. If you planning to have limited types of numbers ie Home, Work, Mobile etcetera I suggest you to add columns for each number. Otherwise make a separate database table "phone_numbers" and relate this to your customer id or similar and add as many numbers you like. Searching will just be both faster and more easy to implement.

如果您仍然需要将它们存储在单个字段中,请在使用php implode()和explode()时建议。在我这样做的情况下,我将使用客户很少想到的字符,例如管道符号 |作为分隔符。当然,如果我的用户不小心在输入中使用了str_replace或类似技术,则最好使用它来过滤掉,但最好是如果很少使用该字符。

If you still need to store them in a single field then use php implode() and explode() as you suggest. In my cases where I do this I would use a character that my customers would rarely think of such as a pipe symbol "|" as a delimiter. Of course I need to use str_replace or such technique to filter out if my users accidentally has used it in the input but best if its a character seldom used.

这篇关于PHP / MySQL:在数据库列中存储多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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