-2

I have a html website with a formular with an action to a php script. What I want to do is to write the data from the formular in a mySQL database on my localhost.

I know the database is accessable and the data is provided to the php script correctly (I'm using MAMP server for database and files). The prepare-statement is correct as far as I could figure it out. The 'id' in the execute statement is an auto-increment, therefore I set it to NULL.

$link is the db-access from db_connection.php

<?php
    include("db_connection.php");
?>

<html>
    <header>
        <meta charset="utf-8">
        <title>Stackoverflow Code</title>
    </header>
    <body>
        <?php
            $name           = $_POST['name'];
            $timeUnits      = $_POST['timeUnits'];
            $description    = $_POST['description'];

            $eintrag = $link->prepare("INSERT INTO `projekt`(`id`,`time-units`, `beschreibung`, `name`) VALUES (?,?,?,?)");
            echo "It's working fine till this line";
            $eintrag->execute([NULL, $timeUnits, $description, $name]);
        ?>
    </body>
</html>

I expected it to write data in the database but what I get is an internal server error (ERROR 500) that instantly vanished from the browser console and a blank website.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141

1 Answers1

-1

You can look at the error log on the webserver to see the mysql error or put execute in a try catch block to catch the exception.

the error is probably from saying id = null since you can't have a null primary key you need to leave out id all together and it will auto increment.

$eintrag = $link->prepare("INSERT INTO `projekt`(`time-units`, `beschreibung`, `name`) VALUES (?,?,?)");
$eintrag->execute([$timeUnits, $description, $name]);

Execute takes the query, puts in the parameters where the "?" are and then runs it. Would link to the docs but without db connect file I dont know if your using mysqli or pdo.

Alo
  • 160
  • 8