lara1
324
8
Laravel

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into `cart` (`product_id`, `product_name`, `product_code`, `product_color`, `price`, `size`, `quantity`, `user_email`, `session_id`)

Posted 4 months ago by lara1

m trying to add to cart a product nut when i click on add to cart button on detail.blade.php page then it shows this error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into cart (product_id, product_name, product_code, product_color, price, size, quantity, user_email, session_id) values (, Filter, ASD111, White, 5000, Choose an option, 1, , )) any solution to resolve this issue

here is detail.blade.php:

  <form name="addtocartForm" id="addtocartForm" action="{{ url('add-cart') }}" 
  method="post">
            {{ csrf_field() }}
            <input type="hidden" name="product_id" value="{{ $productDetails->product_id }}">
            <input type="hidden" name="product_name" value="{{ $productDetails- 
     >product_name }}">
            <input type="hidden" name="product_code" value="{{ $productDetails->product_code 
   }}">
            <input type="hidden" name="product_color" value="{{ $productDetails- 
   >product_color }}">
            <input type="hidden" name="price" value="{{ $productDetails->price }}">
          <div class="w-size14 p-t-30 respon5">
            <h4 class="product-detail-name m-text16 p-b-13">
                {{ $productDetails->product_name }}
            </h4>

            <span class="m-text17">
                PKR: {{ $productDetails->price }}
            </span>
  </form>

code of ProductsController:

  public function addtocart(Request $request){
  $data = $request->all();
  //echo "<pre>"; print_r($data); die;

  if(empty($data['user_email'])){
    $data['user_email'] = '';
  }

  if(empty($data['session_id'])){
    $data['session_id'] = '';
  }

  DB::table('cart')->insert(['product_id'=>$data['product_id'],
  'product_name'=>$data['product_name'],
  'product_code'=>$data['product_code'],
  'product_color'=>$data['product_color'],
  'price'=>$data['price'],
  'size'=>$data['size'],'quantity'=>$data['quantity'],
  'user_email'=>$data['user_email'],
  'session_id'=>$data['session_id']]);
  }

route:

  Route::match(['get','post'], '/add-cart','[email protected]');

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.