MYSQL将逗号分隔值插入单独的字段?

时间:2021-09-10 00:21:34

I know its a strange question, but I have one input field that does a live search for locations. The final value is displayed like this:

我知道这是一个奇怪的问题,但我有一个输入字段可以对位置进行实时搜索。最终值显示如下:

State,Suburb,Post Code

州,郊区,邮政编码

NSW,Sydney,2210

新南威尔士州,悉尼,2210

What I need to do now is split the three values into single values and update them into my separate fields for one row.

我现在需要做的是将三个值拆分为单个值,并将它们更新为我的单独字段中的一行。

I don't want to update multiple rows but just one.

我不想更新多行但只想更新一行。

e.g.:

例如。:

fields ( state | suburb | postcode ) values ( NSW | sydney | 2210 )

fields(state | suburb | postcode)values(NSW | sydney | 2210)

What php commands would I use to split those commas off and create single $values for each item?

我会使用什么php命令来分割这些逗号并为每个项创建单个$值?

5 个解决方案

#1


2  

Use explode on the string.

在字符串上使用爆炸。

$values = 'A,B,C,D';
$values = explode(',', $values);

Each item can then be accessed from an array indexed from 0.

然后可以从索引为0的数组中访问每个项目。

#2


1  

$val = "NSW,Sydney,2210";
$valArr = explode(",", $val);
$query = "UPDATE MyTbl SET State = '$valArr[0]', Suburb = '$valArr[1]', Post_Code = '$valArr[2]' WHERE ...";

#3


0  

The easiest way I think, see the manual for explode:

我认为最简单的方法,请参阅爆炸手册:

$result_array = explode(',', $your_variable);

#4


0  

list($state, $suburb, $postcode) = explode(',', $value);

list($ state,$ suburb,$ postcode)= explode(',',$ value);

#5


0  

Would this work?

这会有用吗?

$header = "State,Suburb,Post Code"
$items = explode(",", $input)
$output = implode("|", $items)

so the output would become State|Suburb|Post Code

所以输出将成为州|郊区|邮政编码

to access each value separately, you can use $items[0], $items[1] ..

要分别访问每个值,您可以使用$ items [0],$ items [1] ..

$data = "NSW,Sydney,2210"
$items = explode(",", $input)
$output = implode("|", $items)

so the output would become NSW|Sydney|2210

所以输出将成为新南威尔士州|悉尼| 2210

#1


2  

Use explode on the string.

在字符串上使用爆炸。

$values = 'A,B,C,D';
$values = explode(',', $values);

Each item can then be accessed from an array indexed from 0.

然后可以从索引为0的数组中访问每个项目。

#2


1  

$val = "NSW,Sydney,2210";
$valArr = explode(",", $val);
$query = "UPDATE MyTbl SET State = '$valArr[0]', Suburb = '$valArr[1]', Post_Code = '$valArr[2]' WHERE ...";

#3


0  

The easiest way I think, see the manual for explode:

我认为最简单的方法,请参阅爆炸手册:

$result_array = explode(',', $your_variable);

#4


0  

list($state, $suburb, $postcode) = explode(',', $value);

list($ state,$ suburb,$ postcode)= explode(',',$ value);

#5


0  

Would this work?

这会有用吗?

$header = "State,Suburb,Post Code"
$items = explode(",", $input)
$output = implode("|", $items)

so the output would become State|Suburb|Post Code

所以输出将成为州|郊区|邮政编码

to access each value separately, you can use $items[0], $items[1] ..

要分别访问每个值,您可以使用$ items [0],$ items [1] ..

$data = "NSW,Sydney,2210"
$items = explode(",", $input)
$output = implode("|", $items)

so the output would become NSW|Sydney|2210

所以输出将成为新南威尔士州|悉尼| 2210