0
Fatal error: Call to a member function prepare() on null in /Users/darryljackson/Desktop/CRUD/create.php on line 10

I am making a simple CRUD application i am currently trying to submit a name and email via a boostrap form. When i click submit i get the error mentioned above. The form should give me a message that says successfully applied, but instead i get the error. I am using PHP and PDO with MYSQL. I keep getting this error and i do not know why.

Create.php
===========
<?php

require 'db.php';
$message = '';

if(isset ($_POST['name']) && isset($_POST['email'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $sql = "INSERT INTO people(names, email) VALUES(:names, :email)";
/*Line 10 */ $statement = $connection->prepare($sql);
    if ($statement->execute([':name' => $name, ':email'=> $email])){
        $message = 'Successfully Applied';
    } 
}
?>

<?php require 'header.php';?>

<div class="container">

<div class="card mt-5">

<div class="card-header">

<h2> Application Information </h2>

</div>

<div class="card-body">

<?php if(!empty($message)): ?>
<div class="alert alert-success">

<?= $message; ?>

</div>
<?php endif; ?>

<form method="post">

<div class="form-group">
<label for="name">Name</label>
<input type="text" name= "name" id = "name" class="form-control">

</div>

<div class="form-group">
<label for="email">Email</label>
<input type="email" name= "email" id = "email" class="form-control">
</div>

<div class="form-group">
<button type = "submit" class="btn btn-info">Apply</button>

</div>

</form>

</div>

</div>

</div>

<?php require 'footer.php';?>
================
db.php

<?php

$dsn = "mysql:host=localhost;dbname=company;";
$username = 'root';
$password = '12345678';
$options = [];
try{
    $connection = new PDO($dsn, $username, $password, $options);

} catch(PDOException $e){


}
=================


I would like for the form to display successfully applied

1 Answers1

1

Whenever you get an error, which says Fatal error: Call to a member function XXX() on null in YYY on line ZZZ, it means your variable on line ZZZ - in your case $connection on line 10 - is (surprise!) NULL.

Now, that gives you specific context - MySQL connection is NULL. Since you did not include the contents of db.php, I will assume you failed to initialize the connection in there.

If you read the docs on PDO connection carefully, you will notice:

Returns a PDO object on success.

PDO::__construct() throws a PDOException if the attempt to connect to the requested database fails.

Seems like you just catch the exception and do not do anything meaningful with it, therefore you can't know if your connection is successful.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • 2
    This. Don't catch an exception _at all_ unless you plan to do something meaningful with it. – Sammitch Aug 26 '19 at 03:46
  • 1
    Wrong. If you don't catch an exception, it will become a fatal error so it makes it inevitable to get 5hevconnection error. What the op is doing is actually *catching* the exception but doing nothing. So as @Sam said - they shouldn't catch – Your Common Sense Aug 26 '19 at 04:23
  • Thank you both for correction. – Kevin Kopf Aug 26 '19 at 09:18