AndyRum
AndyRum

Reputation: 3

PhP TextArea each line with explode and foreach postgresql

It's a very simple question. I have a textarea. In this textarea are names. Every name in a new row.

Brad Pitt
LMFAO
Green Day

and so on...

I would like to put this names in a database. I tried it with explode() and foreach, but it didn't work. :/

Here is the code:

$kilencedik=array();
$kilencedik=explode('\n',$_POST['9']);
foreach($kilencedik as $nev9) {
    $adat9 = pg_escape_string($nev9);
    pg_query($kapcsolodas, "INSERT INTO diakok (nev, ev) values ('$adat9', '9')");
}

I'm using postgreSQL with PHP.

Upvotes: 0

Views: 780

Answers (2)

Yunus
Yunus

Reputation: 67

It's a ' problem change '\n' to "\n".

Upvotes: 2

Mike Brant
Mike Brant

Reputation: 71384

Try using explode(PHP_EOL, $_POST['9']);

Using PHP_EOL is more compatilbe across platforms.

If you don;t want to use that you need to at least use double quotes around the \n instead of single quotes. Using double quotes, PHP will interpret that as the newline character whereas using single quote will look for literal \n as string segment to explode on.

Upvotes: 0

Related Questions