0
votes

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

5
Your error says "Column 'name' cannot be null " which you are leaving empty in your insert.isaace
no i completely insert all inputsHakim LH
try var_dump($my_product)Felipe Valdes
"no i completely insert all inputs" - not according to the error message ...values (, , img ...CD001
What are you getting by dd($request->name,$request->type); under Products method.Shubham Pokhriyal

5 Answers

0
votes

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;
0
votes

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;

0
votes

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.

0
votes

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

0
votes

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