0

I'm using transaction to input data in to two separate tables in my database. Everything works but I just want to know how I can display an error message of what went wrong if the user does not enter all the data required. My code is as follows:

    try {
        $connection->begin_transaction();
        $stmt = $connection->prepare("INSERT INTO images (image, mimetype)
        VALUES (?,?)");

        $file = (file_get_contents($_FILES['file']['tmp_name']));
        $mime = $_FILES["file"]["type"];

        $stmt->bind_param("ss", $file, $mime);
        $connection->query($stmt->execute());

        $stmt = $connection->prepare("INSERT INTO form (time, name, homepage, email, comment) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("sssss", $time, $author, $homepage, $email, $comment);

        $stmt->execute();

        $connection->commit();
    } catch (\Exception $e) {
        $connection->rollback();
        echo "FAILED: " . throw $e->getMessage();
    }
}

But I don´t get the "FAILED: " . throw $e->getMessage();

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Check [the answer by Your Common Sense here](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param). It tells you how to turn MySQL errors into PHP exceptions. – KIKO Software Oct 10 '21 at 09:30
  • I set the "mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);" line in there. But the only error message I get is: Fatal error: Uncaught ValueError: Path cannot be empty in C:\Xampp\htdocs\demo\Uppgifter\6.3\example_1.php:20 Stack trace: #0 C:\Xampp\htdocs\demo\Uppgifter\6.3\example_1.php(20): file_get_contents('') #1 {main} thrown in C:\Xampp\htdocs\demo\Uppgifter\6.3\example_1.php on line 20 – Nilsbergstroom Oct 10 '21 at 09:45
  • 2
    That's not a mysql error, it's a PHP error. `file_get_contents()` has no path in it. – Michel Oct 10 '21 at 10:09

0 Answers0