'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into products (name, type, img_url, updated_at, created_at) values (, , img/products/IMG-20170921-WA0003.jpg, 2017-11-24 15:31:41, 2017-11-24 15:31:41))

PHP Code

 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>

So both Column name & type getting null except img_url, i don't know why? please help



Solution 1:[1]

I am assuming that you are using laravel. To get value of input you can try this code for your name and type field.

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

Solution 2:[2]

i had the same problem, but problem the name of the table was not the same as the name on the form...

$todo->todo = $request->todo;

Solution 3:[3]

yea I faced the same issue with laravel,I had to change my database to nullable then the request went through....going back to my code and adding dd($article),it shows that the form field was empty. I inspected the form field and noticed that the names are not matching with that of the table during migration.

Solution 4:[4]

if you used laravel then clear the catch using PHP artisan optimize: clear then re-run the project, because some catch create such a problem

Solution 5:[5]

One of the reason is that when you declare the variable in inverted comma then check that you are giving space in the declaration as shown in the example ' name' then it will show error:

error

$profileName = $request->input(' name'); ? you have seen that I have given space in variable declare, so, therefore, I have face such a problem whenever you declare variable make sure you should not give space by mistake

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suman B
Solution 2 Steve Muambi
Solution 3 Slycreator
Solution 4 Nikunj Shah
Solution 5 h3t1