jonnypixel
jonnypixel

Reputation: 327

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.

e.g.:

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?

Upvotes: 1

Views: 2784

Answers (5)

cctan
cctan

Reputation: 2023

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

Upvotes: 0

xdazz
xdazz

Reputation: 160843

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

Upvotes: 0

Nick Rolando
Nick Rolando

Reputation: 26167

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

Upvotes: 1

novactown.com
novactown.com

Reputation: 125

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.

Upvotes: 2

jeroen
jeroen

Reputation: 91734

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

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

Upvotes: 0

Related Questions