SQLSTATE [23000]:违反完整性约束:1048列“名称”不能为空(SQL:插入到productsnametypeimg_urlupdated_atcreated_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之外,列名和类型都为null,我不知道为什么?请帮忙

最佳答案

我假设您正在使用laravel。为了获得输入的价值,您可以尝试使用此代码输入您的姓名和类型字段。

$myproduct->name=$request->name;
$myproduct->type=$request->type;

关于php - SQLSTATE [23000]:违反完整性约束:1048列“名称”不能为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47476967/

10-10 13:54