PDO prepared statements stop redirecting when the database connection is in a separate file (config.php). The query works correctly (i.e. inserts properly, etc.), and the redirect works just fine when the database connection is in the document instead of in a separate file but it stops working when the db connection is moved to another file. It's not throwing any error messages.
In order to exclude the other issues that were mentioned in similar questions (error messages and the URL path). I've also removed the error messages (so that it's definitely not printing anything to the document) and tried replacing the relative path with the full URL. When the error reporting is on, it's wrapped in a try/catch block as shown.
UPDATE -
The code below works, but if I replace $conn = new PDO...
with require_once('config.php');
it no longer redirects (regardless of whether or not error messages are being set). I also removed any extra spaces which could be causing a problem, as well as the closing ?>.
I've also tried having the redirect both before and after the catch statement (as well as removing the try/catch block)
<?php
session_start();
$conn = new PDO('mysql:host=localhost;dbname=click2fit', 'db_username', 'DB_Password');
try {
$stmt = $conn->prepare('INSERT INTO customer_info (user_id, fname, lname)
VALUES(:user_id, :fname, :lname)');
$stmt->bindParam(':user_id', $user_id);
$stmt->bindParam(':fname', $_POST['fname'], PDO::PARAM_STR);
$stmt->bindParam(':lname', $_POST['lname'], PDO::PARAM_STR);
$stmt->execute();
header("location: page2.html");
exit();
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
config.php:
<?php
$conn = new PDO('mysql:host=localhost;dbname=click2fit', 'db_username', 'DB_Password');
?>
config.php with error reporting turned on:
<?php
$salt = "XXXXXXXXXXXXXXXXXXX";
try {
$conn = new PDO('mysql:host=localhost;dbname=click2fit', 'db_username', 'DB_Password');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}