duanmei1694 2019-04-28 11:06
浏览 673

如何修复PHP中的“Uncaught PDOException:SQLSTATE [22007]”致命错误

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
    • ¥50 汇编语言除法溢出问题
    • ¥65 C++实现删除N个数据列表共有的元素
    • ¥15 Visual Studio问题
    • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波