shakingspear Posted January 20, 2009 Share Posted January 20, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/ Share on other sites More sharing options...
Lodius2000 Posted January 20, 2009 Share Posted January 20, 2009 try exploding on \r\n linux = \n windows = \r\n Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741587 Share on other sites More sharing options...
shakingspear Posted January 20, 2009 Author Share Posted January 20, 2009 Unfortunately that didn't work. Before I put the "if" statement I was getting a blank line inserted into my table. After the "if" statement nothing. Any chance of an issue there? Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741637 Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741645 Share on other sites More sharing options...
Lodius2000 Posted January 20, 2009 Share Posted January 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741648 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741677 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741709 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741737 Share on other sites More sharing options...
haku Posted January 21, 2009 Share Posted January 21, 2009 You can't use functions inside strings, so your mysql query isn't going to work. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741742 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 So I should implode outside of the $query? Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741751 Share on other sites More sharing options...
haku Posted January 21, 2009 Share Posted January 21, 2009 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741755 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 Gave it a try and it's just adding blank lines to my table. I wish I was smarter... Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741769 Share on other sites More sharing options...
unrelenting Posted January 21, 2009 Share Posted January 21, 2009 Why don't try echoing out the $value before messing with inserting it. That way you can see what your working with. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741774 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 I decided to try to echo out the explode first. The only thing that came up was the word "Array." I didn't enter that word into the form. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741782 Share on other sites More sharing options...
haku Posted January 21, 2009 Share Posted January 21, 2009 That's because explode() creates an array. Instead of echo, use print_r() For example: print_r($lines); Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741792 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 Now I get this (using print_r): Array ( [0] => ) I tried to echo an implode after the explode, I tried to echo out the explode in "foreach," and I tried to print_r in "foreach". Those three tries produced no output. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741804 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 What happens when you echo $_POST['textarea']; Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741806 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 I entered three names: Bill Stephanie Benton It echoed Bill , Stephanie , BentonBill Stephanie Benton Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741808 Share on other sites More sharing options...
haku Posted January 21, 2009 Share Posted January 21, 2009 In that case, try using \r or \r\n as your delimiter in the explode function, then user print_r() to see what you get. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741812 Share on other sites More sharing options...
Zane Posted January 21, 2009 Share Posted January 21, 2009 $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 Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741813 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 Haku: I tried to print with \r and \r\n and got the same Array (0=> ). Zanus: I attempted your code as is, with \r\n, \r, \\n, \\r, and \\r\\n and nothing was put into the table. I then deleted the escape string and tried with \n, \r, \r\n to no avail. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741820 Share on other sites More sharing options...
unrelenting Posted January 21, 2009 Share Posted January 21, 2009 NM Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741822 Share on other sites More sharing options...
Zane Posted January 21, 2009 Share Posted January 21, 2009 Well hell, run $_POST['textarea'] through nl2br and then, explode by " " then you can do a str_replace in your foreach to strip out the \n's and then whatever....mysql_real_escape and all that good secure stuff. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741824 Share on other sites More sharing options...
shakingspear Posted January 21, 2009 Author Share Posted January 21, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741922 Share on other sites More sharing options...
Zane Posted January 21, 2009 Share Posted January 21, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141662-solved-textarea-info-put-into-separate-rows/#findComment-741926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.