php - 从表单中获取值并保存在表的不同行中

标签 php mysql

我有一个表单,可以通过它输入值并将其保存在数据库中

表单代码为

<script>

$(document).ready(function () {

    var sampleTags ;

    $("#myTags").tagit({
        availableTags: sampleTags,
        afterTagAdded:function(){      
          $('[name="tags"]').val($("#myTags").tagit("assignedTags"));
            //alert( $('[name="tags"]').val());
        }
    });
});

</script>

<form action="insert.php">
 <input name="tags" id="myTags" >
  <button class="submit" type="submit" value="submit" >Submit</button>
</form>

insert.php页面代码

$tag = $_POST['tags'];
echo $tag;

它给了我一个看起来像这样的o/p

first,second,third

我想从上面的 o/p 中删除逗号并将它们每个插入到表的单独行中。谁能告诉我如何分离上述o/p

最佳答案

您可以使用explode并使用for循环插入数据库

$tags = explode(",", $_POST['tags']);// convert in array break by comma
foreach($tags as $tag)
{
    // your insert query 
}

关于php - 从表单中获取值并保存在表的不同行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32865467/

相关文章:

基于选定/默认下拉列表的 Php 查询并在同一页面上输出

php - php中mysql查询中的内联变量不起作用

php - 运行新项目时未找到接口(interface) 'Monolog\ResettableInterface'

MySQL 表值与当前日期和上一个日期的差异

mysql - 在 MySQL 中存储散列密码

mysql - rake 数据库 :create generated "if you set the charset manually, make sure you have a matching collation" error

php - 将 "All"选项添加到下拉列表中

javascript - 如何使用 php 将我的星级评分值发送到 mysql 数据库

类似于 sprintf() 的 PHP 重载?

php - 将单元素数组的数组转换为一维数组