逗号分隔的HTML表单输入字段 [英] HTML form input field with comma separated

查看:72
本文介绍了逗号分隔的HTML表单输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在单个文本框中以多个形式输入单词,每个单词之间用逗号(,)分隔,然后根据逗号对单词进行解析,并将每个单词作为单独的记录插入db中

I am trying to figure out how to take multiple word input in a single text box in a form with each word separated by comma(,) and then paring the word based on comma and inserting into db as each word as separate record.

我想到的就是输入

然后使用php的explode功能将单词分开,并存储在db中,但是我不确定如何存储在db中.

and then using explode function of php for separating out the words, and store in db, but I am not sure how to store in db.

推荐答案

我知道将会出现一堆mysql_ *函数答案,因此请不要添加准备好的查询路由.

I know a bunch of mysql_* function answers are going to come in, so ill add the prepared query route.

这并非完美无瑕,但您会明白的

This isn't flawless, but you'll get the idea

// 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();

这篇关于逗号分隔的HTML表单输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆