gpt4 book ai didi

php - 数组到DB插入PHP MYSQL

转载 作者:行者123 更新时间:2023-11-30 00:37:52 25 4
gpt4 key购买 nike

我有一个复选框,它接受一些值,下面的一个是我从矩阵形式获得的后值。现在我下面的数组值应该格式化为这样的

发布值:

Array (
[31_1] => on
[31_2] => on
[31_3] => on
[56_2] => on
[56_4] => on
[66_1] => on
[66_3] => on
)

预期值:

31=>1,2,3

56=>2,4

66=>1,3

如果我能够像这样将值存储在数据库表 (author_book) 中,我会很高兴:

  S.No      Author_ID       Book_IDs
1 31 1,2,3
2 56 2,4
3 66 1,3

简而言之,发布值应该存储在数据库表中,以便我进一步进行。我怎样才能实现这个目标?

最佳答案

此代码将为您构建查询,请记住使用一些数据注入(inject)预防机制

<?php

$x = array (
"31_1" => "on",
"31_2" => "on",
"31_3" => "on",
"56_2" => "on",
"56_4" => "on",
"66_1" => "on",
"66_3" => "on"
);

$newarray = array();

foreach($x as $key => $val){
$key = explode("_", $key);
$newkey = $key[0];
$newval = $key[1];
$newarray[$newkey][] = $newval;
}

foreach($newarray as $key => $val){
$query = "INSERT INTO (Author_ID, Book_IDs) VALUES (" . $key . ",'" . join(',', $val) . "')";
echo $query . "<br />";
}

关于php - 数组到DB插入PHP MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22014639/

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