[英]PHP & MYSQL Insert into database with a textarea (new line & new line into database)?
<?php
require 'db2.php';
?>
<html>
<form action="" method="POST">
<textarea rows="5" cols="80" id="proxies" name="proxies">
</textarea>
<input type="submit" name="insert" value="Insert">
</form>
</html>
<?php
if (isset($_POST['insert']))
{
$string = $_POST['proxies'];
$arrray = explode(':',$string);
$proxy = $arrray[0] . ':' . $arrray[1];
$country = $arrray[2];
$state = $arrray[3];
$city = $arrray[4];
$query = mysqli_query($link,"INSERT INTO `proxies`(`proxy`,`country`, `state`, `city`) VALUES ('{$proxy}', '{$country}', '{$state}', '{$city}')");
}
?>
所以基本上,我很困惑,我以为这会增加每行,但是显然我错了。
我如何让此文本区域每行提交到数据库行中,例如
如果我将其输入到文本区域
test:80:test:test:test
test:80:test:test:test
test:80:test:test:test
test:80:test:test:test
它将添加所有这4条记录,加油!
使用此代码:
$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text); // remove the last \n or whitespace character
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $line) {
// processing here.
}
有关更多说明,请参见此问题 。
请尝试以下代码:
$string = trim($_POST['proxies']);
$stringArray = explode("\n" , $string);
$stringArray = array_filter($stringArray,'trim');
foreach($stringArray as $ $string){
$arrray = explode(':',$string);
$proxy = $arrray[0] . ':' . $arrray[1];
$country = $arrray[2];
$state = $arrray[3];
$city = $arrray[4];
$query = mysqli_query($link,"INSERT INTO `proxies`(`proxy`,`country`, `state`, `city`) VALUES ('{$proxy}', '{$country}', '{$state}', '{$city}')");
}
我建议你追加
PHP_EOL
PHP_EOL-是PHP中的行尾常量,在字符串的末尾附加而不是'\\ n'
我希望它能解决问题,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.