there is already code, but it displays messages about successful addition, even if no data has been added to the database
How to check?

<?php $title = $_POST['title']; $category = $_POST['category']; error_reporting(1); // Отключение вывода стандартных сообщений об ошибках try { $sql = "INSERT INTO foo (title, category) VALUES('$title', '$category')"; $db = new SQlite3("db.sqlite"); $db->exec($sql); if($sql){ echo "Данные успешно сохранены!</br>"; echo $title; } else { echo "Произошла ошибка, пожалуйста повторите попытку."; } } catch (Exception $exception) { echo "Произошла ошибка в строке ", $exception->getLine()-1, ": ", $exception->getMessage(); } ?> 

    2 answers 2

    You somehow strangely check the success of the addition, since the value of the $sql variable does not change in the process, there is a string, it is not empty, because if ($sql) , of course, always returns true .

    Try this code:

     $sql = "INSERT INTO foo (title, category) VALUES('$title', '$category')"; $db = new SQlite3("db.sqlite"); if($db->exec($sql)) { echo "Данные успешно сохранены!</br>"; echo $title; } else { echo "Произошла ошибка, пожалуйста повторите попытку."; } 

    According to the documentation SQlite3::exec returns false in case of an error. If the error was, then it can be obtained by another method: SQLite3 :: lastErrorMsg (returns a text message explaining the last error).

    PS Advice not related to the question, use the prepared statements to protect against injections : SQLite3 :: prepare .

      Try to check for emptiness the values ​​that you add at the very beginning, and if they are not empty, enter them, otherwise Exit .