您好我正在尝试更新数据库中的一行。但是我收到了以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'
(SQL: update `template` set `0` = Module Bro where `template_id` = 0)
这是我的代码:
function csvfileupload(Request $req)
{
if ($req->hasFile('csvfile')) {
$path = $req->file('csvfile')->getRealPath();
$data = \Excel::load($path)->get();
if ($data->count()) {
foreach ($data as $key => $value) {
$arr[] = ['title' => $value->title,
'address' => $value->address,
'intro' => $value->intro,
'mainbody' => $value->mainbody,
'paragraph' => $value->paragraph,
'footer' => $value->footer,
];
}
if (!empty($arr)) {
DB::table('template')->where('template_id', 0)->update($arr);
return "Success";
}
}
}
我想要实现的是更新模板ID为0的数据库中的行,是什么想法?
答案 0 :(得分:0)
放置你的功能
DB :: table(' template') - > where(' template_id',0) - > update($ arr);
在foreach()循环内部并删除make $ arr 1D数组,你的代码在函数内部看起来像这样
if ($req->hasFile('csvfile')) {
$path = $req->file('csvfile')->getRealPath();
$data = \Excel::load($path)->get();
if ($data->count()) {
foreach ($data as $key => $value) {
$arr = ['title' => $value->title,
'address' => $value->address,
'intro' => $value->intro,
'mainbody' => $value->mainbody,
'paragraph' => $value->paragraph,
'footer' => $value->footer,
];
DB::table('template')->where('template_id', 0)->update($arr);
}
if (!empty($arr)) {
return "Success";
}
}
}