gpt4 book ai didi

javascript - ngTagsInput 3.2.0 使用 PHP 插入数组

转载 作者:行者123 更新时间:2023-11-29 18:19:56 27 4
gpt4 key购买 nike

我是初学者,我正在尝试保存支持 use-strings 的 ng-tags-input 版本 3.2.0。我正在尝试通过 PHP 保存标签中的项目。

我经历了内爆,但它没有显示或保存任何东西。如果没有内爆,项目会出现在字符串中。

不知道问题出在哪里。你认为我可能使用了错误的数组吗?

请帮助我。我已经为此苦苦挣扎了 5 天......

[AngularJS]

<tags-input ng-model="tags" use-strings="true"></tags-input>

[ Controller .js]

$scope.tags = [];
$http.post("insert.php", {
tags : $scope.tags})

[插入.PHP]

$data = json_decode(file_get_contents("php://input"));
$tags_array_raw = $data->tags;
$tags_array = implode(", ", $tags_array_raw);
foreach($tags_array as $tags_mid) {
$tags.="$tags_mid";
}
$conn->query("INSERT INTO `table` (tags) VALUES('$tags')") or die(mysqli_error());

除了数组类型之外,普通的INPUT都保存得很好。

我不知道从这里该去哪里,请就这个“标签输入数组保存”问题向我提供建议。我希望这也能帮助其他人。

提前谢谢您!!

最佳答案

不要将字段保存为 json 或逗号分隔的列表,这会破坏规范化的第一条规则 (1NF),而是考虑为“数组”创建另一个与此类似的表:

create table child(
parent_id int
field ...

)

这样您就可以通过执行以下操作取回数据:

select * from child where parent_id = ...

希望有帮助!

关于javascript - ngTagsInput 3.2.0 使用 PHP 插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com