我正在编写一个简单的web应用程序,用户可以在其中将数据输入到文本区域输入字段,然后将数据放入数据库。
I.例如:项目1、项目2、项目3、项目4、项目5、项目6
显然,文本区域将是用户输入的任何内容。我需要所有的项目,输入到我的数据库中,但输入到它们自己的行中。我见过很多网站做类似的事情,你把一堆信息输入到一个文本区域,然后用逗号分隔,然后放进数据库。
谢谢你的帮助,我非常感谢。
$array = explode(',', $_POST['textarea']); // this will contain an array of each item $array[0] will be item1 etc.
你可能也想用trim((处理逗号后面的空格https://www.w3schools.com/php/func_string_ltrim.asp这样就不会在数据库中留下空白。
然后插入语句:
$statement = $PDO->prepare("INSERT INTO table(column1) VALUES(:item)");
foreach($array as $item) {
//SQL to insert the array element
$statement->execute(array(
"item" => $item
));
} //Something like this will do the trick...
这有道理吗?