I have problem with add new data to database using PDO. I have a form where there are several fields. If I leave an empty field then an error pops up. The problem occurred when mysql was updated on the server. On another server and locally the problem does not appear. Setting the server in all forms to default values of 0 in the fields will be very difficult.
Database version: 10.3.13-MariaDB-100.cba.1-log - Debian testing
Please, check if you do not complete all the fields and generate an error.
Is it ok when I leave empty input then error generated?
PHP Code:
$config = require_once 'config.php';
try {
$db = new PDO(
"mysql:host={$config['host']};dbname={$config['database']};charset=utf8",
$config['user'],$config['password'],
[PDO::ATTR_EMULATE_PREPARES=>false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
}
catch(PDOException $error)
{
echo $error->getMessage();
exit('Database error!');
}
if (isset($_POST['dodaj'])) {
$pole1 = $_POST['pole1'];
$pole2 = $_POST['pole2'];
$pole3 = $_POST['pole3'];
$pole4 = $_POST['pole4'];
$query = $db->prepare("INSERT into pola VALUES (:pole1, :pole2, :pole3, :pole4)");
$query->bindValue(':pole1', $pole1, PDO::PARAM_INT);
$query->bindValue(':pole2', $pole2, PDO::PARAM_INT);
$query->bindValue(':pole3', $pole3, PDO::PARAM_INT);
$query->bindValue(':pole4', $pole4, PDO::PARAM_INT);
$query->execute();
}
HTML code:
<form method="post" action="#">
<input type="text" id="pole1" name="pole1">
<input type="text" id="pole2" name="pole2">
<input type="text" id="pole3" name="pole3">
<input type="text" id="pole4" name="pole4">
<input type="submit" name="dodaj" value="dodaj">
</form>
Database:
pole1 | pole2 | pole3 | pole4
Fatal error: Uncaught PDOException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column
********
.pola
.pole3
at row 1 in ************************new 1.php:18 Stack trace: #0 ********************new 1.php(18): PDOStatement->execute() #1 {main} thrown in ******************************new 1.php on line 18