将textarea换行符分隔为不同的数据库行 [英] Separating textarea line breaks into different database rows

查看:82
本文介绍了将textarea换行符分隔为不同的数据库行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea表单输入,用户可以在其中输入食谱的成分。我最初拥有的是一个动态的,基于Javascript的添加成分字段,但意识到这对可访问性不利。最初,我是将每个输入字段都分离到数据库中它自己的行中,并且想知道如何使用textarea做到这一点?

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.

其他详细信息

我正在使用PHP并使用'post'变量。

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.

任何人都可以帮忙吗?

答案

$variable = $_POST['fieldName'];

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


推荐答案

您可以按换行符分割文本:

you can split the text by the linebreaks:

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

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

所以现在您有了一个要循环的数组,将每个数组项分别保存到数据库中

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

这篇关于将textarea换行符分隔为不同的数据库行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆