I'm setting up a form to submit data into a MySQL database using PDO (which I am not that familiar with).
This is the code to input the data from a simple input form with Name, Birthday and two hardware options to choose from:
<?php
session_start();
if (isset ($_POST['next_1']))
{
$name = $_POST['Name'];
$geburtsdatum = $_POST['Geburtsdatum'];
$hardware = $_POST['hardware'];
$true = "1";
}
else
{
header('Location: pdo_test.php', true, 302);
}
?>
<!DOCTYPE html>
<html lang="de">
<meta charset="UTF-8">
<head>
<title>PDO-Test</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<body>
<img id="logo" src="bilder/some_logo.png" alt="some Logo">
<h1>PDO Test Seite</h1><br>
<?php
//Showing Errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "<br>";
//Verbindung zur Datenbank
include 'includes/connect.php';
echo "<br>";
echo "<b>Name: </b>" . $name . "<br><b>Geburtsdatum: </b>" . $geburtsdatum . "<br>" ;
foreach ($hardware as $key => $ho)
{
switch($ho)
{
case "Phone";
echo "Phone: " . $ho . "<br>";
break;
case "Headset";
echo "Headset: " . $ho . "<br>";
break;
}
}
try
{
$conn->beginTransaction();
$stmt = $conn->prepare("INSERT INTO testdb(name, geburtsdatum, phone, headset) VALUES(:name, :geburtsdatum, :phone, :headset)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':geburtsdatum', $geburtsdatum);
foreach ($hardware as $key => $ho)
{
switch($ho)
{
case "Phone";
$stmt->bindParam(':phone', $true);
break;
case "Headset";
$stmt->bindParam(':headset', $true);
break;
}
}
$stmt->execute();
$conn->commit();
echo "<br>New records created successfully";
}
catch(PDOException $e)
{
$conn->rollBack();
error_log("Database rollback; inserting data failed: ".$e->getMessage());
}
?>
</body>
</html>
I'm using the echo
part to double-check the input from the form (can be ignored for this problem, everything working fine there).
Looking around in different posts similar to my problem, I only found 'solutions' how to enable error reporting.
In my connect.php (include 'includes/connect.php';
) I have already enabled error reporting as following: $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
and in the main script I added
error_reporting(E_ALL);
ini_set('display_errors', 1);
For some reasons, the only output i receive on my website is the echo
stuff and the 'Connected successfully' line from my connection script.
I don't receive any errors, but at the same time my data is not getting inserted into my database.
Any ideas how to get the error messages to show up are appreciated as well as any solutions that help me with getting the data into my database.