gpt4 book ai didi

php - Laravel 从 textarea 中获取每一行并插入数据库中的新行

转载 作者:可可西里 更新时间:2023-11-01 08:03:29 28 4
gpt4 key购买 nike

我试图从 textarea 中获取每一行并插入到数据库中的新行中。所以,当我写一个带有新刹车线的文本列表时 f.ex:
测试1
测试2
测试3

我想将这些多行中的每一个插入到 MySql f.ex 中的新行中:
id kwName
1. 测试1
2. 测试2
3. 测试3

请帮帮我。

我的代码是:

Blade

<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">

Controller

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

这只是您根据需要使用它的示例。

关于php - Laravel 从 textarea 中获取每一行并插入数据库中的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41937629/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com