Separating textarea line breaks into different database rows

泄露秘密 提交于 2019-12-12 10:01:47

问题


I have a textarea form input where users input ingredients for a recipe. What I originally had was a dynamic, Javascript based 'add an ingredient' field but realised this was not good for accessibility. Originally I was separating each input field out into it's own row in my database and was wondering how I could do this with a textarea?

What I assume is done is separating out each line break and placing it into it's own row in the table but I do not know the syntax for that.

Further Details

I'm using PHP and processing the textarea with a 'post' variable.

The reason for wanting to put each ingredient in it's own row is because I've been informed that using relational databases for unpredictable amounts of text is the way to go.

Can anyone help?

Answer

$variable = $_POST['fieldName'];

$variable_array = split("\n", $variable);
foreach($variable_array as $for)
    {
Insert into db
    }

回答1:


you can split the text by the linebreaks:

$data_array = split("\n", $data);

so now you have an array to loop over, saving each array item into the db separately



来源:https://stackoverflow.com/questions/5497515/separating-textarea-line-breaks-into-different-database-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!