0

I am trying to do an insert to my MySQL database with PHP PDO. There is my connection code on my connection file located at ../Info/Connection.php

<?php
    $user = "InsertDB";
    $pass = "Passwword123";
    $dbh = new PDO('mysql:host=localhost;dbname=SQL10', $user, $pass);
?>

I am using an insert on another page. Here is the insert code on my form page at ../Events/Add.php

<?php
    include "../Info/Connection.php";
?>
<html>
<head>
  <meta charset="UTF_8">
</head>
<body>
  <H1>Add Event</H1>
  Add An Event.<br/ >
  <hr />
<?php

    if (!empty($_POST["Submit"])) {
      $EventDate = $_POST['YYYYOfEvent']."-".$_POST['MMOfEvent']."-".$_POST['DDOfEvent'];
      $TodayDate = date("Y-m-d");
      $statement = $pdo->prepare('INSERT INTO techdates (DateOfEvent, Event, Assigned, Status, DateOfUpdate) VALUES (:dateofevent, :event, :assigned, :status, :dateofupdate)');
      $statement->execute([
          'dateofevent' => $EventDate,
          'event' => $_POST['Event'],
          'assigned' => $_POST['Assigned'],
          'status' => $_POST['Status'],
          'dateofupdate' => $TodayDate
      ]);
    }
 else {
}
?>
</body>
</html>

For the prepare line I am getting the following error:

Fatal Error: Uncaught Error: Call to a member function prepare() on null in File.php:17 Stack trace: #0 {main} thrown in File.php on line 17.

I have never seen this error before but this is preventing the insert. Do I have syntax issue somewhere? I am pretty sure I have connection to my DB without an issue. What do I seem to be missing.

EDIT: I have copied the connection information to the Add.php page and now it works. I am wondering if my include statement is missing something. It is as follows.

<? php 
include "../Info/Connection.php";
global $pdo;
?>

EDIT2: Added the updated actual code.

JukEboX
  • 355
  • 1
  • 3
  • 16
  • You do have a missing `'` in `'dateofupdate => $TodayDate]` – Nigel Ren Nov 15 '19 at 18:28
  • 1
    Are you actually including the connection file on the "other page"? It isn't shown in the code above. – Patrick Q Nov 15 '19 at 18:29
  • 1
    Sounds like `$pdo` isn't defined properly, which is a sign it didn't get declared like you thought it did. – tadman Nov 15 '19 at 18:32
  • 1
    If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Nov 15 '19 at 18:33
  • @NigelRen Yes sorry that is a typo. – JukEboX Nov 18 '19 at 15:39
  • @PatrickQ Yes I am using an include statement to the connection file at the top of the page. – JukEboX Nov 18 '19 at 15:39
  • @tadman Thanks. I will copy my connection string and see if it is making a valid connection. – JukEboX Nov 18 '19 at 15:40
  • 1
    @JukEboX It seems that what you have above is not your _actual_ code. Could you show us _that_, so we are better informed? – Patrick Q Nov 18 '19 at 16:11
  • @PatrickQ You were right about the $pdo statement not being called. Unless my include statement isn't working or my global $pdo isn't working. – JukEboX Nov 18 '19 at 19:23
  • 1
    @JukEboX You don't need to use `global $pdo;` unless it is within a function where `$pdo` isn't passed in as an argument. Generally speaking, using global vars is discouraged, and even the term "global" is mostly a misnomer in PHP. The code in `Connection.php` is run in the same scope as whatever file includes it. I can't really be any more specific regarding your particular case without seeing the _actual_ code being used, instead of heavily abridged and recreated versions. TLDR: your use of `global $pdo;` is probably unnecessary and likely the cause of the problem. – Patrick Q Nov 18 '19 at 19:34
  • @PatrickQ thanks. Looks like the global causing confusions with the connection setting. Please post your comment as the answer and I will make the official answer. :) – JukEboX Nov 18 '19 at 20:26
  • 1
    @JukEboX The question was already closed as a duplicate. Answers cannot be posted on closed questions. You might find it helpful to read the PHP manual entry on [variable scope](https://www.php.net/manual/en/language.variables.scope.php). – Patrick Q Nov 18 '19 at 20:28
  • 1
    in your code I see you're initializing $dbh. but using $pdo. could it be the cause of your problem? – ursa Nov 18 '19 at 20:44
  • @ursa yeah that was also part of it. – JukEboX Nov 18 '19 at 21:23

0 Answers0