perky416 Posted March 26, 2011 Share Posted March 26, 2011 Hi Everyone, Hopefully this will be a quick one. How would i write each line in a textbox as a new row in a mysql database? I cant seem to find the solution on google. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/231805-write-each-textarea-line-as-a-new-row-in-database/ Share on other sites More sharing options...
perky416 Posted March 26, 2011 Author Share Posted March 26, 2011 I have tried using the code below which i found in another post however nothing writes to the database. if ($_POST['submit']) { $values = $_POST['textarea_name']; $array = split("\r\n", $values); foreach($array as $line) { mysql_query("INSERT INTO table_name('field_name') VALUES($line)"); } } Quote Link to comment https://forums.phpfreaks.com/topic/231805-write-each-textarea-line-as-a-new-row-in-database/#findComment-1192678 Share on other sites More sharing options...
harristweed Posted March 27, 2011 Share Posted March 27, 2011 split From manual> Warning This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. Quote Link to comment https://forums.phpfreaks.com/topic/231805-write-each-textarea-line-as-a-new-row-in-database/#findComment-1192780 Share on other sites More sharing options...
trq Posted March 27, 2011 Share Posted March 27, 2011 Field identifiers are not strings (are not surrounded by quotes) in SQL. You should also enable error reporting when developing and always attempt to catch errors in your code, you have done nothing to check if your query succeeded or to indicate why it has failed. Quote Link to comment https://forums.phpfreaks.com/topic/231805-write-each-textarea-line-as-a-new-row-in-database/#findComment-1192782 Share on other sites More sharing options...
ignace Posted March 27, 2011 Share Posted March 27, 2011 split From manual> Warning This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. Doesn't mean it won't work. It's as thorpe stated, quoting the wrong value and no error catching. if (isset($_POST['submit'])) { $lines = split("\r\n", $_POST['textarea_name']); if(!empty($lines)) { foreach($lines as $line) { mysql_query("INSERT INTO table_name(field_name) VALUES('".mysql_real_escape_string($line)."')") or trigger_error('MySQL: '.mysql_error(), E_USER_ERROR); } } } Quote Link to comment https://forums.phpfreaks.com/topic/231805-write-each-textarea-line-as-a-new-row-in-database/#findComment-1192791 Share on other sites More sharing options...
perky416 Posted March 27, 2011 Author Share Posted March 27, 2011 Thanks guys i got it to work. I did try it without the quotes in the first place but it still did not work. After reading into the deprecated split() iv decided to go with explode(). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/231805-write-each-textarea-line-as-a-new-row-in-database/#findComment-1192828 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.