簡體   English   中英

Laravel從textarea獲取每一行並插入數據庫中的新行

[英]Laravel get each of lines from textarea and insert into new row in database

我試圖從textarea獲取每一行並插入數據庫中的新行。 所以,當我用新的brakelines f.ex編寫一個文本列表時:
TEST1
TEST2
TEST3

我想將這些多行中的每一行插入到MySql f.ex的新行中:
id kwName
測試1
2. test2
3. test3

請幫我。

我的代碼是:

<textarea rows="1" name="kwName" class="form-control" placeholder="Insert keywords list"></textarea>
<input type="submit" class="btn btn-primary" name="add_kw" value="save">

調節器

if ($request->has('add_kw')) {

                $this->validate($request,[
                  'kwName'=> 'required',
                ]);

                // create new data              

                $values = new keyword;

                $values->kwName = $request->kwName;
                $values->website_id = $id;
                $values->save();
}

我想這會對你有所幫助::

<?php
  $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. 
  }
?>

這只是根據您的需要使用它的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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