简体   繁体   中英

MYSQL insert comma separated values into separate fields?

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

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.

eg:

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?

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.

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

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

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

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

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] ..

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

so the output would become NSW|Sydney|2210

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM