tp6插入数据库报错
本帖最后由 comcn 于 2022-9-7 14:35 编辑网页提示:
SQLSTATE: Insert value list does not match column list: 1136 Column count doesn't match value count at row 45
百度了下:https://blog.51cto.com/shijianfeng/5153742,是因为字段数量不同,
因为我插入的数据是通过接口获取的,只能后期追加数组字段这种方法了
方法:https://www.php.cn/php-ask-478202.html,这个只能解决一半,有个逻辑问题
比如我获取到json数据是这样的:
{
"qq": 25741687,
"ww": "ww",
"ee": "",
"rr": "2022-09-02 15:33:21",
"tt": "b20daaf62f44fb894acba6fbe91a8580",
},
{
"qq": 25741687,
"ww": "ww",
"ee": "",
"rr": "2022-09-02 15:16:40",
"tt": "02216e73d16b4f5de17d220fa8e11d50",
"yy": "百度",
"uu": "江苏",
"ii": 0,
},
需要先判断数据是否包含yy、uu、ii字段,如果有就跳过不插入,如果没有,就插入这三个字段,并赋值为空,可
以参数上面php中文网的代码
$arr = ['age' => 11];
$list = array(
['id'=>1,'name'=>'aaa'],
['id'=>2,'name'=>'bbb'],
['id'=>3,'name'=>'ccc']
);
array_walk($list, function (&$value, $key, $arr) {
$value = array_merge($value, $arr);
},$arr);
var_dump($list);
tp6中代码如何实现呢?
$str = '';
$jsonArr = json_decode($str, true);
$sql = [];
foreach ($jsonArr as $v) {
$v['yy'] = $v['yy'] ?? null;
$v['uu'] = $v['uu'] ?? null;
$v['ii'] = $v['ii'] ?? null;
$sql[] = $v;
}
if (!empty($sql)) {
Db::table('name')->insertAll($sql);
} 勤勤学长 发表于 2022-9-5 11:04
$str = '';
$jsonArr = json_decode($str, true);
谢谢大佬,我试下
页:
[1]