简体   繁体   中英

How to resolve PHP Laravel Sql Insert error?

When I press the buy button I get this error

QueryException in Connection.php line 729:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'qty' cannot be null (SQL: insert into orders ( user_id , product_id , qty , updated_at , created_at ) values (3, 1, , 2018-10-06 20:44:27, 2018-10-06 20:44:27))

first of all, I'm a beginner so I'm still learning and I do not know if some of this is correct, all I want is when I press the buy button it will display the form and I will type the quantity then submit and my quantity will increase and the products inventory will decrease and if I deleted my order, The products will increase again (I did this and I think it will work) and the user can update his quantity and if someone ordered something the seller cannot delete it, only update his inventory as same as the buy button way and the form, and if the users ordered all products it will display (out of stock) with a dead link button, I am training, It is not homework and I do not know how to write all functions..

this is the Buy button

<li><span><a href="/add_order/{{ $product['id'] }}">Buy</a></span></li>

which should pop up the next form when I press it,

this is my form code for the buy button

<form method="POST" action="/add_order/{{ $product['id'] }}" class="klaviyo_subscription_form" enctype="multipart/form-data">

    {{ csrf_field() }}

    <input type="hidden" name="g" value="LIST_ID">

    <div class="klaviyo_fieldset">
        <p class="klaviyo_header">Please define quantity you need, then order it.</p>
    </div>

    <div class="klaviyo_fieldset">
        <div class="klaviyo_field_group{{ $errors->has('qty') ? ' has-error' : '' }}">
            <label for="k_id_modal_$email" style="display: block; text-align:center">Quantity</label>               
            <input type="number" id="k_id_modal_$email" name="qty" style="display: block; margin:auto">

            @if ($errors->has('qty'))
                <span class="help-block">
                    <strong>{{ $errors->first('qty') }}</strong>
                </span>
            @endif
        </div>
    </div>

    <div class="klaviyo_fine_print"></div>

    <div class="klaviyo_form_actions">
        <button type="submit" class="klaviyo_submit_button">
            <span>Order Now</span>
        </button>
    </div>

    <div class="klaviyo_below_submit" ></div>
</form>

OrderController

public function store(Request $request, $id)
{
    $product = Product::find($id);
    $order = new Order();
    $order->user_id = Auth::user()->id;
    $order->product_id = $id;
    $order->qty = $request->get('qty');
    $product->inv = $product->inv - $order->qty;
    $order->save();
    $product->save();

    return redirect('/');
}

thanks for reading :D

Request is object and not array! You need to use method to get request value. Check updated code

  public function store(Request $request, $id)
  {
    $product = Product::find($id);
    $order = new Order();
    $order->user_id = Auth::user()->id;
    $order->product_id = $id;
    $order->qty = $request->get('qty');
    $product->inv = $product->inv - $order->qty;
    $order->save();
    $product->save();
    return redirect('/');
  }

This will work if logic for passing "qty" to request is correct. You can do dump of request and die inside store function, to make sure about this fact.

Your Problem is you sending the data with the POST method.

But you access the data from the GET method.

OrderController

$order->qty = $request->get('qty');   // this is where you are wrong.

Change this either,

$order->qty = $request->post('qty');

or,

$order->qty = $request->qty // this is the preferred approach.

In your database table filed "qty" set default value null or as default 0. and write

$order->qty = $request->qty;

it will work fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM