【发布时间】:2018-05-08 16:29:00
【问题描述】:
SQLSTATE[23000]:完整性约束违规:1048 列“名称”不能为空(SQL:插入
products(name,type,img_url,updated_at,created_at)值(, , img/products/IMG-20170921-WA0003.jpg, 2017-11-24 15:31:41, 2017-11-24 15:31:41))
PHP 代码
public function Products(Request $request){
if($request->isMethod('post'))
{
if(Input::hasFile('file')){
$myproduct = new Product();
$myproduct->name=$request->input('name');
$myproduct->type=$request->input('type');
$file = Input::file('file');
$url='img/products/'.$file->getClientOriginalName();
$file->move('img/products',$file>getClientOriginalName());
$myproduct->img_url = $url;
$myproduct->save();
return view("controlpanel.products");
}
}}
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table>
<tr>
<form action="{{ URL('products') }}" method="post" enctype="multipart/form-data" class="container">
<td>
<label for="name">Product Name</label>
<input type="text" name="name" class="form-control" id="name" required>
</td>
<td>
<label for="type">Product Type</label>
<select class="form-control" name="type" id="type">
<option value="type1">type 1</option>
<option value="type2">type 2</option>
</select>
</td>
<td>
<label for="file">Select Product image :</label>
<input type="file" name="file" id="file">
</td>
<td>
<input type="submit" value="Upload" name="submit" id="sub1">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</td>
</form>
</tr>
</table>
</body>
</html>
所以除了 img_url 之外,列名和类型都为空,我不知道为什么?请帮忙
【问题讨论】:
-
您的错误说“列'名称'不能为空”,您在插入时将其留空。
-
不,我完全插入所有输入
-
试试 var_dump($my_product)
-
“没有我完全插入所有输入” - 不是根据错误消息
...values (, , img ... -
Products 方法下
dd($request->name,$request->type);得到了什么。