0

I'm trying to save the links that are entered in the text area field. The links are seperated by a new line/enter. So in the example I made here below, he should add 3 lines in the table for each url link. But for some reason he isn't doing that.

TEXT AREA

www.url1.com

www.url2.com

www.url3.com

HTML

<textarea id="url" name="url"></textarea>

PHP

$url = isset($_POST['url']);
$links_array = explode('\n', $url);

foreach($links_array as $url)
{
    $query3 = mysql_query("INSERT INTO links(sigaren_id, link) VALUES ((SELECT id FROM sigaren ORDER BY id DESC LIMIT 1), '$url')");
}

Thanks in advance!

Kind regards

user3360972
  • 107
  • 2
  • 13

2 Answers2

1

Instead of using single quotes, use double quotes around the \n string

if(isset($_POST['url'])){
    $links_array = explode("\n", $_POST['url']);

    foreach($links_array as $url)
    {
        $query3 = mysql_query("INSERT INTO links(sigaren_id, link) VALUES ((SELECT id FROM sigaren ORDER BY id DESC LIMIT 1), '$url')");
    }
}

EDIT: Also, you're setting $url to isset($_POST['url']) which returns a true and false value, NOT the actual content of the variable being assessed, I've changed my answer to reflect this.

Here's a great answer describing the different in how PHP processes 'content' strings and "content" strings https://stackoverflow.com/a/3446286/3944304

Community
  • 1
  • 1
CT14.IT
  • 1,635
  • 1
  • 15
  • 31
0

You need to use double quotes to define newlines as characters:

"\n"

Single quotes notate strings, double quotes notate characters

$post_keys = explode("\n", $_POST['url']);
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59