/    Sign up×
Community /Pin to ProfileBookmark

Split insert textarea value per line and insert each line as one record

I need to know how to do the following using php coding please

1.take inserted text from a textarea
2.split the content per line
3.Insert each line into the database as its own row with own id number
Can anyone show me how to do this please?

THis is what I have tried so Far:

Below is my form page [U]display_form.php[/U]:

[code=html]
<form method=”post” action=”display_db.php”>
Member ID:
<input name=”mid” type=”text” value=””><br/><br/>
Enter Keywords:
<input name=”keywords” type=”text” value=””><br/><br/>
Enter Your Links :
<textarea name=”links” rows=”3″ cols=”20″></textarea><br/><br/>

<input type=”submit” id=”button” value=” Submit “/>

</form>[/code]

And below is my [U]display_db.php[/U]:

[code=php]
<?php
$con=mysqli_connect(“localhost”,”root”,””,”test”);
// Check connection
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

if(isset($_POST[“data”]))
{
$line_data = explode(“n”, $_POST[“keywords”]);
foreach($line_data as $key => $value)
{
$sql = “INSERT INTO table (display) VALUE(‘{$value}’)”;
mysql_query($sql);
}
}
if (!mysqli_query($con,$sql))
{
die(‘Error: ‘ . mysqli_error($con));
}
echo “1 record added”;

mysqli_close($con);
?>
[/code]

Ant kind of help is appreciated.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@seven7Dec 14.2013 — HI, if you want to get every line in the textarea you have to USE this:
[code=php]
f(isset($_POST['data']))
{
$line_data = explode("n", $_POST['links']);
}

add name to submit:
[code=php]
<input type="submit" id="button" value=" Submit " name="data"/>
[/code]
Copy linkTweet thisAlerts:
@NogDogDec 14.2013 — I'm a bit confused, as your textarea is named "links", which you don't reference in your code, while you try to explode on "keywords", which is a text-type input field, which should not allow newlines. Assuming you really want to do the exploding and inserting of the "links" data:
[code=php]

$data = preg_split('s*[rn]+s*#', trim($_POST['links']));
$values = array();
foreach($data as $val) {
$values[] = "('".mysqli_real_escape_string($con, $val)."')";
}
$sql = "INSERT INTO table (display) VALUE
".implode(",n", $values);
[/code]
×

Success!

Help @priyankagound spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...