我想弄清楚如何在一个表单中的单个文本框中输入多个单词,每个单词用逗号(,)分隔,然后根据逗号分割单词并将每个单词作为单独的记录插入数据库.
我想到的是接受输入
然后用php的explode函数分离出单词,存入db,但不知道怎么存入db。
最佳答案
我知道一堆 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();
关于php - 以逗号分隔的 HTML 表单输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11860936/