In my test environment, which is MAMP 4.5, the statement works perfectly with no issues. But when I use the same statement on my test VM the INSERT fails, without error.
I am using this to catch any errors:
try {
// http://php.net/manual/en/pdo.connections.php
$dbConn = new PDO("mysql:host={$databaseHost};dbname={$databaseName}", $databaseUsername, $databasePassword);
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting Error Mode as Exception
// More on setAttribute: http://php.net/manual/en/pdo.setattribute.php
} catch(PDOException $e) {
echo $e->getMessage();
}
And here is my INSERT statement:
<?php
// Database connection file
include_once("../config.php");
if(isset($_POST['Submit'])) {
$status=$_POST['status'];
$system=$_POST['system'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$primary_num=$_POST['primary_num'];
// Insert data into table
$sql = "INSERT INTO table(
status,
system,
first_name,
last_name,
primary_num
) VALUES(
:status,
:system,
:first_name,
:last_name,
:primary_num
)";
$query = $dbConn->prepare($sql);
$query->bindParam(':status', $status);
$query->bindParam(':system', $system);
$query->bindParam(':first_name', $first_name);
$query->bindParam(':last_name', $last_name);
$query->bindParam(':primary_num', $primary_num);
$query->execute();
// Redirect to the display page
header("Location: index.php");
}
?>
Fix:
I set the default values for each column in my database (except the Primary Key) to be NULL and now the data posts successfully!