如何从文本区域字段插入mysql多个字段
我需要一些帮助来弄清楚如何从具有多行的文本区域在 mysql 数据库上添加多个字段。我希望将每一行分为 6 个值(数据库的每个字段一个值)。
例如,我有以下几行:
info1|info2|info3|info4|info5|info6
info1|infob|infoc|infod|infoe|infof
info1|info8|info9|info0|info1|info2
info1|info4|info4|info5|info6|info7
每个字段都用“|”分隔(那是因为我在网上找到的例子,将在这里发布一些。:)
然后我有以下文件:insert_form.php
<form action="insert_engine.php" method="post">
<p>
<textarea name="pctext" id="pctext" cols="100" rows="10"></textarea>
</p>
<p>
<input type="submit" />
</p>
</form>
并且我有insert_engine.php:
<?php
$con = mysql_connect("localhost","USER","PASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DBNAME", $con);
// assuming the text area value is in $_GET["pctext"]
$lines = explode("\n", $_GET["pctext"]);
foreach($lines as $line) {
list($FIELD1, $FIELD2, $FIELD3, $FIELD4, $FIELD5, $FIELD6) = explode(" | ", $line);
$sql="INSERT INTO TABLENAME (FIELD1, FIELD2, FIELD3, FIELD4, FIELD5, FIELD6)
VALUES
('$_POST[FIELD1]', '$_POST[FIELD2]', '$_POST[FIELD3]','$_POST[FIELD4]', '$_POST[FIELD5]', '$_POST[FIELD6]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
}
mysql_close($con)
?>
<meta http-equiv="refresh" content="2;URL=confirmation.php" />
说实话,我几乎不了解php,我正在学习,强行查看在线示例,然后在我的 Linux 上进行测试,等等。我从这里的另一个线程中得到了这个:将文本从textarea插入MySQL数据库而不丢失格式我正在努力让它发挥作用。
我知道我正在连接到MySQL,选择数据库,从pctext(文本区域)获取内容,分解每一行并用“|”分隔(管道),然后使用循环(foreach)插入表中。
当我点击表单上的提交按钮时,我进入引擎页面,2秒后我进入确认页面,我的数据库中没有插入任何内容,但我的注册表是空的,所以发生了一些事情。
有人可以帮我写这个脚本吗?
提前谢谢你
电脑
I need some help to figure out how to add multiple fields, on a mysql db, from a textarea with multiple lines. I would like to have each line to be broken into 6 values (one for each field of my db).
for example, I have the following lines:
info1|info2|info3|info4|info5|info6
info1|infob|infoc|infod|infoe|infof
info1|info8|info9|info0|info1|info2
info1|info4|info4|info5|info6|info7
each field is separated with a "|" (thats because of the example I found online, will post here in a few.:)
then I have the following file: insert_form.php
<form action="insert_engine.php" method="post">
<p>
<textarea name="pctext" id="pctext" cols="100" rows="10"></textarea>
</p>
<p>
<input type="submit" />
</p>
</form>
and I have the insert_engine.php:
<?php
$con = mysql_connect("localhost","USER","PASS");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DBNAME", $con);
// assuming the text area value is in $_GET["pctext"]
$lines = explode("\n", $_GET["pctext"]);
foreach($lines as $line) {
list($FIELD1, $FIELD2, $FIELD3, $FIELD4, $FIELD5, $FIELD6) = explode(" | ", $line);
$sql="INSERT INTO TABLENAME (FIELD1, FIELD2, FIELD3, FIELD4, FIELD5, FIELD6)
VALUES
('$_POST[FIELD1]', '$_POST[FIELD2]', '$_POST[FIELD3]','$_POST[FIELD4]', '$_POST[FIELD5]', '$_POST[FIELD6]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "record added";
}
mysql_close($con)
?>
<meta http-equiv="refresh" content="2;URL=confirmation.php" />
To be really honest I barely know about php, I am learning, by force, looking at examples online, testing then on my Linux, etc. I got this one from another thread from here: Inserting text from textarea into MySQL database without losing formatting and I am trying to put it to work.
I understand that I am connecting to the MySQL, selecting the DB, getting the content from pctext (textarea), exploding each line and breaking by "|" (pipe) and then, using a loop (foreach), inserting into the TABLE.
When I click the submit button on the form, I go to the engine page and after 2 seconds i got to the confirmation page, nothing is inserted on my DB but I do have a empty registry, so something is going on.
Could anyone help me with this script?
thank you in advance
PC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
因为您的表单方法设置为“post”而不是“get”
也更改
为
因为您的示例文本在 | 周围没有空格
另外.. list 不会将元素转换为 $_POST vars
应该是
('$FIELD1', '$FIELD2', '$FIELD3','$FIELD4', '$FIELD5', '$FIELD6')";
should be
Because your form method is set to 'post' and not 'get'
Also change
to
Because your example text doesn't have spaces around the |
Also.. list doesn't turn the elements into $_POST vars
Should be
('$FIELD1', '$FIELD2', '$FIELD3','$FIELD4', '$FIELD5', '$FIELD6')";
您不仅使用 $_GET 而不是 $_POST,而且还缺少常量的语言行为,应该引用数组键,但在您的情况下,您使用 $_POST["FIELD1"] 而不是 $FIELD1
Not only you're using $_GET instead of $_POST but you are missing language behaviour for constants too, array keys shoul be quoted, but in you case you using $_POST["FIELD1"] instead of $FIELD1