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