Notice: Trying to get property 'blood_group' of non-object Call Stack #TimeMemoryFunctionLocation 10.0010408992

I am trying to add some features on an old project. But this happens when i try to edit some data. Any Idea where I am getting wrong.

My code


<?php 
//session already set in config file
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include('config/db.php');



$id = $_GET['id'];

$sql = "SELECT * FROM bloodgroups WHERE id=:id";

$query = $dbh -> prepare($sql);

$query -> execute([':id' => $id ]);

$result = $query ->fetch(PDO::FETCH_OBJ);


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  
    $blood_group = $_POST['blood_group'] ;

    $sql = "UPDATE bloodgroups set blood_group = :blood_group WHERE id=:id";
    
    $query = $dbh -> prepare($sql);

    if ($query -> execute([':blood_group' => $blood_group, ':id' => $id])) {
      
      header("location: editBloodGroup.php");
    }
     
}

?>

<?php include('routes/header.php');?>

<?php include('routes/topbar.php');?>

<?php include('routes/sidebar.php');?>



<div class="content-wrapper">
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
          </div>
        </div>
      </div>
    </div>
      

<section class="content">
 <div class="container-fluid">
  <div class="row">
    <div class="col-8">

      <div class="card">
        <div class="card-header">
          EditBlood 
        </div>
        <div class="card-body">
          <form class="form-inline" action=""  id="form" method="POST">
          <div class="form-group mx-sm-3 mb-2">
            <input type="text" value="<?= $result->blood_group; ?>" name="blood_group" id="blood_group" class="form-control" >
          </div>
          <button type="submit" class="btn btn-primary mb-2">Submit</button>
        </form>
        
        </div>
 </div>
 
 </div>
   </div>
 </div>
  </section>
 </div>
  
<?php include_once 'routes/footer.php';?>

i dont’t see what your problem is with understanding the error message. you can debug every variable with var_dump().

When your form submits, and you execute the code inside this if() clause:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

won’t there be no value for $id at that point? In turn that would cause the query to fail, but your code doesn’t check whether it ran or not before using the results.

Done that code is working perfectly…

The same code works in my older system, without any errors.

Does it happen when you bring up the form to edit the data, or when you try to store the edited data?

then you have to start the debug process again, use var_dump()

When i click the edit button the select id is selected thus the data is populated but editing it is the challenge now.

the error

The other problem you’ll have in there is that by the time you come to process the form submission, the $id variable will have gone away.

What do you get if you var_dump($POST) just inside the if($_SERVER clause?

editBloodGroup.php:24:string ‘POST’ (length=4)

Sorry, typo, I meant var_dump($_POST); to see the contents of the form array.

C:\wamp64\www\Raw\bloodbank\backend\editBloodGroup.php:46:
array (size=2)
‘title’ => string ‘B+’ (length=2)
‘submit’ => string ’ ’ (length=0)

The problem is you named the field title but are expecting blood_group.

It was just another try out which i did and forgot to change… The code i posted above is the one I am using. Sorry for that.

And what does the var_dump() show when you use the actual code, not the code you were trying out?

The output

C:\wamp64\www\Raw\bloodbank\backend\editBloodGroup.php:46:
array (size=2)
‘title’ => string ‘B+’ (length=2)
‘submit’ => string ’ ’ (length=0)
[/quote]

I thought that was with the code you were trying out, where you’d changed the form field name from “blood_group” to “title”? If not, then there’s your answer - you don’t have a form field called “blood_group”, and that’s why you get the error.

Alright…

Is this the right way the code should be or I should add some code???

When you say “this”, what code are you referring to? The code you posted earlier is not producing that var_dump() output because the field names don’t match the fields in the source code. Post the code you have now.