【发布时间】:2010-10-21 19:07:05
【问题描述】:
我正在尝试创建一个 php 脚本,该脚本将粘贴到 textarea 中的 HTTP 链接输入到数据库中的单独行中。更确切地说:
第一页是textarea(name=textarea-linkfield)所在的地方,链接会粘贴在这里
http://www.stackoverflow.com
http://www.yahoo.com
....
http://google.com
链接正在使用$linkfield=$_POST['textarea-linkfield']; 转移到php 脚本中,我希望将链接插入到数据库中,每行每个http 链接。数据库名:站点,表名犁,列:id,url,...
L.E.我尝试了作为概念证明:
$linkfield=$_POST['textarea-linkfield'];
$strip=explode("\n",$linkfield);
echo $strip[1];
但我收到 500 内部服务器错误
L.E.2
答案:
// Split the string into pieces
$pieces = explode("\n", str_replace(array("\n", "\r\n"), "\n", trim($linkfield)));
// Build the top of the INSERT query
$sql = "INSERT INTO `plow`(`url`) VALUES\n";
// Build the rest of the ;INSERT query by re-assembling the
// pieces.
$sql .= "('";
$sql .= implode("'), ('", $pieces);
$sql .= "')";
mysql_query($sql) or die ('Error: ' . mysql_error());
mysql_close();
感谢大家的帮助。 克里斯。
【问题讨论】:
-
"\n"上的explode()应该可以工作,尝试一下,如果不成功,请尽最大努力返回一些代码。 -
我更新了问题。谢谢,克里斯。
-
您提交的表单中是否包含文本区域?
-
看看 PHP 的错误报告功能,看看是否可以从错误中挤出更多信息:php.net/manual/en/function.error-reporting.php
-
是的。常规形式,没什么花哨的。