Jump to content

[SOLVED] Textarea info put into separate rows


shakingspear

Recommended Posts

I've been trying to make this work for a while now, but I'm still not getting it. I want to take the information from different lines in a textarea and put it in different database rows. I only have two columns on my table. One is an auto-increment integer. The input would be like this:

 

Example 1

Example 2

Example 3

 

I'm not getting errors, but it's also not writing to the table. I would appreciate any help. Thanks!

 

Here's the script:

<?php
$textarea = mysql_real_escape_string($_POST['textarea']);
$array = explode("\n", $textarea);

$i=0;
$value = trim($array[$i]);
if (!empty($value)) {
foreach ($array as $value) {
mysql_query("INSERT INTO random_prompts (prompt) VALUES ('{$array[$i]}')") or die(mysql_erorr());
$i++;
}
}

?>

 

And here's the html for the form:

 

<h2>Data Import</h2>
<form methods="post" action="">
Data: <textarea name="textarea" rows="20" cols="30"</textarea>

<input type="submit" name="submit" value="Import History">
</form>

 

Link to comment
Share on other sites

Try this:

<?php
$textarea = $_POST['textarea'];

$lines = explode("\n", $textarea);

foreach ($lines as $line)
{
    if(!empty(trim($line)))
    {
        $values[] = '(' . mysql_real_escape_string((trim($line)) . ')';
    }
}

$query = "INSERT INTO random_prompts (prompt) VALUES (" . implode(', ', $values) . ")";
mysql_query($query) or die(mysql_erorr());

?>

Link to comment
Share on other sites

hehe I hope this isnt the problem and is just a transcription typo but

 

<textarea name="textarea" rows="20" cols="30"</textarea>

should be

<textarea name="textarea" rows="20" cols="30"></textarea>

// text area needs a closing angle bracket

 

//mjdamato provides a good change for your php also

Link to comment
Share on other sites

I really appreciate your help, and I'm trying to figure this out without being a huge bother. I tried the code and got a few errors. I figured out a couple of them but am stuck again with this error:

 

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/public_html/addtodatabase3.php on line 17

 

Fatal error: Call to undefined function mysql_erorr() in /home/public_html/addtodatabase3.php on line 18

 

I'm not sure what this means or how to address it. I'll repost the code with my changes in case that has anything to do with it. The only major change I had to make was with the empty() statement. According to the manual you can't use it the way it was written by mjdamato.

 

Again, thank you very much for your help.

 

<? php
$textarea = $_POST['textarea'];

$lines = explode("\n", $textarea);

foreach ($lines as $line)
{
$pleasework = trim($line);
    if(!empty($pleasework))
    {
        $values[] = '(' . mysql_real_escape_string((trim($line))) . ')';
    }
}

$query = "INSERT INTO random_prompts (prompt) VALUES (" . implode(', ', $values) . ")";
mysql_query($query) or die(mysql_erorr());
?>

Link to comment
Share on other sites

Ok, I figured out that "error" was spelled wrong. The new error code is this:

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/public_html/addtodatabase3.php on line 17

Column count doesn't match value count at row 1

 

The table is two columns so I thought that adding the "id" column would help. This is how I did it:

$query = "INSERT INTO random_prompts (id, prompt) VALUES ("", "  . implode(', ', $values) . ")";
mysql_query($query) or die(mysql_error());

 

I now have this message:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/public_html/addtodatabase3.php on line 17

 

Thoughts?

Link to comment
Share on other sites

I'm closer, but still not there. I have the code to where it doesn't show any errors. When I input data into the form every entry comes out like this:

 

,).

 

 

Again, here's my code with the adjustments. I'm so close...

 

<?php

$textarea = $_POST['textarea'];

$lines = explode("\n", $textarea);

foreach ($lines as $line)
{
$pleasework = trim($line);
    if(!empty($pleasework))
    {
        $values[] = '(' . mysql_real_escape_string((trim($line))) . ')';
    }
}

$query = "INSERT INTO random_prompts (id, prompt) VALUES ('  . implode(', ', $values) . ')";
mysql_query($query) or die(mysql_error());

?>

Link to comment
Share on other sites

$textarea = mysql_real_escape_string($_POST['textarea']);
$array = explode("\n", $textarea);

$i=0;
$value = trim($array[$i]);
if (!empty($value)) {
foreach ($array as $value) {
   mysql_query("INSERT INTO random_prompts (prompt) VALUES ('{$array[$i]}')") or die(mysql_erorr());
   $i++;
}
}
?>

Your are using mysql_real_escape_string which I believe will turn \n into \\n

so try exploding by \\n or \\r\\n

 

but really, you should first explode it and THEN run mysql_real_escape

Link to comment
Share on other sites

<?php
$textarea = mysql_real_escape_string($_POST['textarea']);
$array = explode("\n", $textarea);

$i=0;
$value = trim($array[$i]);
if (!empty($value)) {
foreach ($array as $value) {
   mysql_query("INSERT INTO random_prompts (prompt) VALUES ('{$array[$i]}')") or die(mysql_erorr());
   $i++;
}
}
?>

Your are using mysql_real_escape_string which I believe will turn \n into \\n

so try exploding by \\n or \\r\\n

 

but really, you should first explode it and THEN run mysql_real_escape

 

I got this to work! But I have one more question. You said to first explode and then run the escape string. I tried it a few different ways with no luck (everything still entered the database, but the string didn't work). Could you show how it should be written?

 

Thank you very much for all of your help. I don't know what the problem was as the code is basically the same. I just built it line by line and echoed it out until I got to the end.

Link to comment
Share on other sites

You said to first explode and then run the escape string. I tried it a few different ways with no luck (everything still entered the database, but the string didn't work). Could you show how it should be written?

well this was my idea of it

$textarea = explode("\n", $_POST['textarea']);

foreach ($textarea as $value) {
   $clean_info = mysql_real_escape_string(trim($value))
   $theQuery .= "INSERT INTO random_prompts (`prompt`) VALUES ('"  . $clean_info .  "')\n";
}
$queryIt = mysql_query($theQuery) or die("Error:" . mysql_error() . "
\n" . $theQuery);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.