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');

//Editform query to populate data to the form

$id = $_GET['id'];

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

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

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

$result = $query ->fetch();

//Edit query to update the data 

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: index.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';?>

The error

The error means that $result isn’t an object. Given the php error related settings being shown, this is most likely due to the SELECT query not matching any data.

Two things -

  1. You must validate all inputs before using them. If a required input is not present, you should set up and display a message telling the user so, and not run any code that’s dependent on that input.
  2. If a query doesn’t match any data, you should set up and display a message for the user telling them so, and not run any code that’s dependent on the result from the query.

Since there are no (apparent) php errors associated with $_GET[‘id’], it is set, but either is empty or contains a value that doesn’t match any data. What is the code that produces the link/form that submits $_GET[‘id’]?

The one I have posted above… The code I have used is the same code I have used in the Old system and it’s perfectly working well… When I try it here errors…

Sure… I will do that…

There’s nothing in the posted code that produces a link/form that would cause $_GET[‘id’] to be set.

When this code executesdata will be populated in the input form which it does.


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

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

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

$result = $query ->fetch();

//populated data

<input type="text" name="title" value="<?php echo $result['blood_group']?>" >

Then we edit the data in the form & update it with this code below...


   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: index.php");

    }
     
}

you hit ctrl+f, you search for id - no input field or URL key with this name is found.

Where should i search specifically???

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

Sorry this was another try out…not what i initially posted above…

Sponsor our Newsletter | Privacy Policy | Terms of Service