HTML 表单输入字段,以逗号分隔

2023-12-03

我试图弄清楚如何在一个表单中的单个文本框中输入多个单词,每个单词用逗号(,)分隔,然后根据逗号对单词进行配对,并将每个单词作为单独的记录插入到数据库中。

我想到的是接受输入

然后使用php的explode函数分离出单词,并存储在数据库中,但我不知道如何存储在数据库中。


我知道将会出现一堆 mysql_* 函数答案,因此我将添加准备好的查询路径。

这并不完美,但你会明白的

// Get the array of words
$words = explode( ',', $formname );

// Create an array of :word1, :word2, :word3, etc for use in binding
foreach( range( 1, count( $words ) ) as $wordnumber ) {
    $bind[] = ':word'.$wordnumber;
}

// Create the sql query
$sql = sprintf( "insert into table ( word ) values ( %s )", implode( '),(', $bind ) );

// Prepare the query
$stmnt = $pdo->prepare( $sql );

// Bind each word
foreach( $words as $k => $word ) {
    $stmnt->bindValue( ":word" . ++$k, $word );
}

// Execute
$stmnt->execute();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTML 表单输入字段,以逗号分隔 的相关文章