如何将每个单词或获取字符串作为单独的条目添加到数据库
How to add each word or get string as separate entry to database
$stuffname=$_GET['stuffname'];
$pieces = explode(" ", $stuffname);
$pieces 中可以包含任意数量的单词。
我想循环此查询,以便将片段中的每个词作为单独的条目添加到 table 关键字中
$query=$con->prepare("INSERT INTO keywords (stuffname) VALUES (?)");
$query->execute([$pieces]);
感谢您的宝贵时间
不清楚您是否尝试过任何操作或卡在什么地方,但这应该很简单,只需一个循环即可。
两者都
- 为每个新行执行单独的 INSERT
$query = $con->prepare("INSERT INTO keywords (stuffname) VALUES (?)");
foreach ($pieces as $piece)
$query->execute([$piece]);{
}
或
- 尝试构建具有多组值的单个 INSERT:
$sql = "INSERT INTO keywords (stuffname) VALUES ";
$vals = "";
foreach ($pieces as $piece)
{
$vals .= ($vals != "" ? "," : "")."(?)";
}
$query = $con->prepare($sql.$vals);
$query->execute([$pieces]);
$stuffname=$_GET['stuffname'];
$pieces = explode(" ", $stuffname);
$pieces 中可以包含任意数量的单词。
我想循环此查询,以便将片段中的每个词作为单独的条目添加到 table 关键字中
$query=$con->prepare("INSERT INTO keywords (stuffname) VALUES (?)");
$query->execute([$pieces]);
感谢您的宝贵时间
不清楚您是否尝试过任何操作或卡在什么地方,但这应该很简单,只需一个循环即可。
两者都
- 为每个新行执行单独的 INSERT
$query = $con->prepare("INSERT INTO keywords (stuffname) VALUES (?)");
foreach ($pieces as $piece)
$query->execute([$piece]);{
}
或
- 尝试构建具有多组值的单个 INSERT:
$sql = "INSERT INTO keywords (stuffname) VALUES ";
$vals = "";
foreach ($pieces as $piece)
{
$vals .= ($vals != "" ? "," : "")."(?)";
}
$query = $con->prepare($sql.$vals);
$query->execute([$pieces]);