我已经创建了一个注册表单。我想将数据插入数据库和文件,图像到文件夹。当我插入数据和文件或图像时,它已成功插入。但是当我尝试没有文件或图像时,它会给我这个错误:
(2/2)QueryException
SQLSTATE [HY000]:常规错误:1364字段'fileimg'没有 默认值(SQL:插入
academic
(name
,username
,pw
) 价值观(Kisalka Rajapaksha,960863801V, $ 2Y $ 10 $ u0Ryg3Yi9wYma.wm5piP2.1HFHKxruo6gZDU2Fo1onwQJhJmOWhG。))
我该如何解决这个问题?
Controller.php文件。
function insert(Request $req)
{
$this->validate($req, [
'name' => 'required'
]);
if(Input::hasFile('file_img')){
$name = $req->input('name');
$username = $req->input('username');
$password = $req->input('password');
$passen = bcrypt($password);
$file = Input::file('file_img');
$fileimg = $file->getClientOriginalName();
$destinationPath = 'img';
$rules = array(
'file_img' => 'required|max:10000|mimes:doc,docx,jpeg,png,jpg'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// redirect our user back with error messages
$messages = $validator->messages();
// send back to the page with the input data and errors
return Redirect::to('RegShort')->withInput()->withErrors($validator);
return redirect('RegShort');
}
else if ($validator->passes()) {
$filemove = $file->move($destinationPath, $fileimg);
$dataimg = array("name"=>$name,"username"=>$username,"pw"=>$passen,"fileimg"=>$fileimg,"filemove"=>$filemove);
DB::table('academic')->insert($dataimg);
$req->session()->flash('Msg', 'Successfully Inserted !!');
return redirect('RegShort');
}
}
else
{
$names = $req->input('name');
$usernames = $req->input('username');
$passwords = $req->input('password');
$passens = bcrypt($passwords);
$data = array("name"=>$names,"username"=>$usernames,"pw"=>$passens);
DB::table('academic')->insert($data);
$req->session()->flash('Msg', 'Successfully Inserted !!');
return redirect('RegShort');
}
}
答案 0 :(得分:1)
为fileimg
列设置UNIQUE或NOT NULL
对于列,您应该允许NULL
试试这个:ALTER TABLE academic MODIFY fileimg VARCHAR(255);
答案 1 :(得分:1)
您应该将fileimg设置为迁移文件中的可空字段。
$table->string('fileimg')->nullable();
这样你就可以保存没有fileimg的条目。