Home > Net >  Attempt to assign property "PName" on null
Attempt to assign property "PName" on null

Time:10-07

I'm making a CRUD App, Everything is working fine except Updation. I want to update data in my MySQL table.

Controller code:

function update(Request $req){

    $ID = $req->get('update_id');
    $Name = $req->get('update_name');
    $Price = $req->get('update_price');
    $new_prod = product::find($ID);
    $new_prod->PName = $Name;
    $new_prod->PPrice = $Price;
    echo $new_prod;
    $new_prod->save();
    return redirect('/');
} 

Update Blade code:

<form action="updatedata" method="get">
    @csrf
    <div >
        <label for="">Product ID</label>
        <input type="text"   value="{{$pid}}" name="update_id" disabled>
    </div>
    <div >
        <label for="">Product Name</label>
        <input type="text"  value="{{$pname}}" name="update_name">
    </div>
    <div >
        <label for="">Product Price</label>
        <input type="text"   value="{{$price}}" name="update_price">
    </div>
   
    <br>
    <button type="submit" >Update</button>
</form> 

SQL QUERY:

select * from `products` where `products`.`Id` is null limit 1 

CodePudding user response:

Try this:

function update(Request $req){

    $ID = $req->get('update_id');
    $Name = $req->get('update_name');
    $Price = $req->get('update_price');

    $new_prod = product::find($ID);
    if ($new_prod) {
        $new_prod->update([
          'PName' => $Name,
          'PPrice' => $Price  

        ]);
    }else{
        echo "No data found for this "  $ID;
    }
    return redirect('/');
} 

CodePudding user response:

Your $new_prod variable is null, meaning the result of product::find($ID); is null. The reason for this likely being Laravel could not find a record in your database with the given Id.

If you trace things backwards to where you set $ID, you're getting it from your $request object (note it is good practice to never assume user input is valid and therefore validate incoming data) which suggests the value you're getting for $ID from your $request is invalid.

If you take it back another step and look at your <form> and the update_id input, you've set the input to disabled. Fields flagged with the disabled attribute are not sent in the request when the form is submitted. Therefore, replace disabled with readonly. That should allow the update_id to be submitted with the <form>.

It would also be a good idea to perform some checks after you've performed your find query for a product to ensure it actually has a value before you go using it.

CodePudding user response:

you can validation id first , like this:

$validator = $this->getValidationFactory()->make($request->all(), ['update_id' => 'required|integer|exists:tabel_name,id']);
if ($validator->fails()) {

$this->throwValidationException($request, $validator);

}
//your code

CodePudding user response:

This code is not secure! You must reassure yourself that update_id is present, and check that for the value $ID a record exists in the database. Laravel validation can help you

Nothing reassures that $ID exists, the $new_prod either. But you can try this:

You have to be sure that $new_prod exists and is not NULL before proceeding with the update in the database.

This code should help you

  function update(Request $req){

    $ID = intval($req->get('update_id')); //intval is not required
    $Name = $req->get('update_name');
    $Price = $req->get('update_price');
    //Will show a 404 error if a record doesn't exist
    $new_prod = product::findOrFail($ID);
    //the code can be executed
    $new_prod->PName = $Name;
    $new_prod->PPrice = $Price;
    // Don't do this, because you have a redirect below
    echo $new_prod;
    $new_prod->save();
    return redirect('/');
}

CodePudding user response:

if($ID!=null || $ID!=""){
 DB::table('products')->where('id', $ID)->update([
  'db_product_column_name_name' => $Name,
  'db_product_column_price_name' => $Price,
 ]);
}else{
 dd("ID null");
}

Check if the ID is null or empty and update if it is.

  • Related