php - 以逗号分隔的 HTML 表单输入字段

标签 php mysql html

我想弄清楚如何在一个表单中的单个文本框中输入多个单词,每个单词用逗号(,)分隔,然后根据逗号分割单词并将每个单词作为单独的记录插入数据库.

我想到的是接受输入

然后用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/

相关文章:

php - 用表中一行的值填充数组

java - 在 Android 中为我的应用抛出 IllegalStateException

mysql - 即使没有记录也每小时行

c# - 如何使用链接服务器连接两个表

html - Dreamweaver 网页上烦人的横幅

php - 来自 mysql 的 IDs 来自同时来源的大量插入

php - 将工具提示添加到 css 打印文件表

java - 如何修复此递归 StackOverFlowException 错误?

html - 父元素添加了不必要的空格

javascript - jQuery为具有相同src的图像添加alt标签