Cant write into DB

I want to store the input from my website inside a database on the same server. Previously i tested it with a smaller one, it works. Then i build it in a new page, no error, but also no input in my db. Maybe someone can help me:

<?php
    $nachname = $_POST["Nachname"];
    $vorname = $_POST["Vorname"];
    $food = $_POST["food"];
    $nachname_b = $_POST["Nachname_B"];
    $vorname_b = $_POST["Vorname_B"];
    $food_b = $_POST["food_B"];
    $Ankunft = $_POST["Anreisedatum"];
    $Kinder = $_POST["Kinder"];
    $mail = $_POST["Mail-Adresse"];

    $host = "127.0.0.1:3306";
$dbname = "XXX";
$username = "XXX";
$password = "XXX";

$conn = mysqli_connect($host, $username, $password, $dbname);

if (mysqli_connect_errno()) {
    die("Connection error: " . mysqli_connect_error());
}   

$sql = "INSERT INTO Guestlist (nachname, vorname, food, nachname_b, vorname_b, food_b, Ankunft, Kinder, mail)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = mysqli_stmt_init($conn);

if ( ! mysqli_stmt_prepare($stmt, $sql)) {
 
    die(mysqli_error($conn));
}


mysqli_stmt_bind_param($stmt, "sssssssss",
                       $vorname,
                       $nachname,
                       $food,
                       $vorname_b,
                       $nachname_b,
                       $food_b,
                       $Ankunft,
                       $Kinder,
                       $mail);

mysqli_stmt_execute($stmt);

echo mysqli_stmt_affected_rows($stmt);

echo "Record saved.";

I tried it with other code, same result.

>Solution :

The fastest way to understand what’s wrong – is to see the error. I would recommend you use the following piece of code at the start of your document to display all errors that happened:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Also, my personal recommendation is to use PDO instead of the outdated mysqli extension for database connection.
There is a good guide about PDO: https://phpdelusions.net/pdo

Leave a Reply