Php 拆分逗号分隔的输入框值,然后存储在数据库中
我有一个文本输入框,您可以在其中键入以逗号分隔的标记。我想将这些标签分别存储在一个表中,但我不知道如何做到这一点。 我知道我必须分开后,他们张贴标签,所以我现在有这个Php 拆分逗号分隔的输入框值,然后存储在数据库中,php,sql,Php,Sql,我有一个文本输入框,您可以在其中键入以逗号分隔的标记。我想将这些标签分别存储在一个表中,但我不知道如何做到这一点。 我知道我必须分开后,他们张贴标签,所以我现在有这个 $array = explode(',', $_POST['tag']); 这是一个好的开始吗?在这之后我必须如何继续?从输入字段创建数组,就像在代码中一样 $array=explode(',',$\u POST['tag']) 使用foreach循环创建mysql多插入查询 与此类似: $id=mysqli_inser
$array = explode(',', $_POST['tag']);
这是一个好的开始吗?在这之后我必须如何继续?从输入字段创建数组,就像在代码中一样
$array=explode(',',$\u POST['tag'])代码>
使用foreach循环创建mysql多插入查询
与此类似:
$id=mysqli_insert_id($con);//get your project id here
$sql="";
foreach($array as $tag_name){
//modify below to add $id along with $tag_name
$sql.="('{$id}','{$tag_name}'),"; // you need to remove last comma else it will throw mysql error
}
if($sql!=""){
//rtrim to remove last ',' from string.
$sql=rtrim($sql,',');
$sql="INSERT INTO tags_tbl (project_id,tag) VALUES {$sql};"`
}
现在使用mysqli\u query($con,$sql)
在数据库中插入值从输入字段创建数组,就像在代码中一样
$array=explode(',',$\u POST['tag'])代码>
使用foreach循环创建mysql多插入查询
与此类似:
$id=mysqli_insert_id($con);//get your project id here
$sql="";
foreach($array as $tag_name){
//modify below to add $id along with $tag_name
$sql.="('{$id}','{$tag_name}'),"; // you need to remove last comma else it will throw mysql error
}
if($sql!=""){
//rtrim to remove last ',' from string.
$sql=rtrim($sql,',');
$sql="INSERT INTO tags_tbl (project_id,tag) VALUES {$sql};"`
}
现在使用mysqli\u query($con,$sql)
在数据库中插入值从输入字段创建数组,就像在代码中一样
$array=explode(',',$\u POST['tag'])代码>
使用foreach循环创建mysql多插入查询
与此类似:
$id=mysqli_insert_id($con);//get your project id here
$sql="";
foreach($array as $tag_name){
//modify below to add $id along with $tag_name
$sql.="('{$id}','{$tag_name}'),"; // you need to remove last comma else it will throw mysql error
}
if($sql!=""){
//rtrim to remove last ',' from string.
$sql=rtrim($sql,',');
$sql="INSERT INTO tags_tbl (project_id,tag) VALUES {$sql};"`
}
现在使用mysqli\u query($con,$sql)
在数据库中插入值从输入字段创建数组,就像在代码中一样
$array=explode(',',$\u POST['tag'])代码>
使用foreach循环创建mysql多插入查询
与此类似:
$id=mysqli_insert_id($con);//get your project id here
$sql="";
foreach($array as $tag_name){
//modify below to add $id along with $tag_name
$sql.="('{$id}','{$tag_name}'),"; // you need to remove last comma else it will throw mysql error
}
if($sql!=""){
//rtrim to remove last ',' from string.
$sql=rtrim($sql,',');
$sql="INSERT INTO tags_tbl (project_id,tag) VALUES {$sql};"`
}
现在使用mysqli\u查询($con,$sql)
在数据库中插入值在google中搜索使用php在sql中插入数据。我知道如何做,但不知道如何存储分隔的值。您需要循环使用$array
并插入每个值在google中搜索使用php在sql中插入数据。我知道如何做,但不知道如何存储分隔的值值。您需要在$array
中循环,并在google中插入每个值搜索,以便使用php在sql中插入数据。我知道如何执行此操作,但不知道如何存储分隔的值。您需要在$array
中循环,并在google中插入每个值搜索,以便使用php在sql中插入数据。我知道如何执行此操作,但我不知道如何存储分隔的值。您需要在$array
中循环并插入每个值非常感谢您的帮助。我现在有了这个代码:$id=mysqli\u insert\u id($con);foreach($tag_name形式的数组){$sql.=“('{$tag_name}')”;}if($sql!=”)$sql=“插入标记(项目id,标记)值('$id','$sql')”$query2=mysqli_query($con,$sql);}不会将任何内容插入到标记表中。我做错了什么?让我更改我的答案以添加项目?非常感谢您的帮助。我现在有了这个代码:$id=mysqli\u insert\u id($con);foreach($tag_name形式的数组){$sql.=“('{$tag_name}')”;}if($sql!=”)$sql=“插入标记(项目id,标记)值('$id','$sql')”$query2=mysqli_query($con,$sql);}不会将任何内容插入到标记表中。我做错了什么?让我更改我的答案以添加项目?非常感谢您的帮助。我现在有了这个代码:$id=mysqli\u insert\u id($con);foreach($tag_name形式的数组){$sql.=“('{$tag_name}')”;}if($sql!=”)$sql=“插入标记(项目id,标记)值('$id','$sql')”$query2=mysqli_query($con,$sql);}不会将任何内容插入到标记表中。我做错了什么?让我更改我的答案以添加项目?非常感谢您的帮助。我现在有了这个代码:$id=mysqli\u insert\u id($con);foreach($tag_name形式的数组){$sql.=“('{$tag_name}')”;}if($sql!=”)$sql=“插入标记(项目id,标记)值('$id','$sql')”$query2=mysqli_query($con,$sql);}不会将任何内容插入到标记表中。我做错了什么?让我更改答案以添加项目id