簡體   English   中英

PHP和MYSQL插入具有textarea的數據庫(將新行和新行插入數據庫)?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM