【问题标题】:PHP transaction commit OK but unchanged mySql databasePHP事务提交OK但未更改mySql数据库
【发布时间】:2016-04-23 00:37:33
【问题描述】:

我在从 php 脚本向 mySql db 中插入记录时遇到问题。这是我的代码:

// Set fields for post record insertion
$insertPostSql = "INSERT INTO tl_posts(title, city, city_id, video_url, thumbnail_url, description, longitude, latitude, user_id, category, hotel_id, restaurant_id, activity_id, creation_date, qualified) 
VALUES(:title, :city, :cityId, :video, :thumbnail, :description, :long, :lat, :userid, :category, :hotelId, :restoId, :activityId, :crea, :checked)";
$insertPostStmt = $pdo->prepare($insertPostSql);
$insertPostStmt->bindValue(':title', $title);
$insertPostStmt->bindValue(':city', $city);
$insertPostStmt->bindValue(':cityId', $cityId);
$insertPostStmt->bindValue(':video', substr($videoPath, strlen(ROOT_PATH)));
$insertPostStmt->bindValue(':thumbnail', substr($thumbnailPath, strlen(ROOT_PATH)));
$insertPostStmt->bindValue(':description', $description);
$insertPostStmt->bindValue(':long', $longitude);
$insertPostStmt->bindValue(':lat', $latitude);
$insertPostStmt->bindValue(':userid', $userId);
$insertPostStmt->bindValue(':category', $category);
$insertPostStmt->bindValue(':hotelId', ($category === 'hotel' ? $categoryId : "NULL"));
$insertPostStmt->bindValue(':restoId', ($category === 'restaurant' ? $categoryId : "NULL"));
$insertPostStmt->bindValue(':activityId', ($category === 'activity' ? $categoryId : "NULL"));
$insertPostStmt->bindValue(':crea', $now);
$insertPostStmt->bindValue(':checked', $checked);

// New post transaction
$pdo->beginTransaction();
try {
    $insertPostStmt->execute();
    $pdo->commit();
    $response['result']['database'] = "Post added successfully";
} catch (Exception $e) {
    $pdo->rollBack();
    $response['result']['database'] = "Error! Post not saved in database";
    die("Error! ".$e->getMessage());
}

它导致“发布添加成功响应”,但没有任何内容保存到数据库中。 每个值都可以,我之前已经回显过它们进行测试。 SQL 语法也可以,因为它适用于数据库 SQL 命令面板。 基本上,所有字段都是 Varchar。除了 'id' 和最后两个 Datetime 和 Boolean 之外的类型。

我不得不说我之前使用过这个语句并且它工作正常,所以数据库也可以:

$insertSql = "INSERT INTO tl_posts(title, city, video_url, thumbnail_url, description, longitude, latitude, user_id, category, creation_date, qualified)
                VALUES(:title, :city, :video, :thumbnail, :description, :long, :lat, :userid, :category, :crea, :checked)";
$insertStmt = $pdo->prepare($insertSql);
$insertStmt->bindValue(':title', $title);
$insertStmt->bindValue(':city', $city);
$insertStmt->bindValue(':video', substr($videoPath, strlen(ROOT_PATH)));
$insertStmt->bindValue(':thumbnail', substr($thumbnailPath, strlen(ROOT_PATH)));
$insertStmt->bindValue(':description', $description);
$insertStmt->bindValue(':long', $longitude);
$insertStmt->bindValue(':lat', $latitude);
$insertStmt->bindValue(':userid', $userId);
$insertStmt->bindValue(':category', $category);
$insertStmt->bindValue(':crea', $now);
$insertStmt->bindValue(':checked', $checked);

我很好奇找到关于它的最后一句话。感谢您的帮助

【问题讨论】:

  • 注意:你需要捕获正确的异常,即PDOException而不是Exception

标签: php mysql pdo insert commit


【解决方案1】:

您可能没有正确捕捉错误。您必须确保 PDO 报告级别正常。

在你的 pdo 初始化之前添加这些行:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

您可能还需要捕获正确的 pdo 异常

try{
...
}
catch(PDOException $err)
{
...
}

我觉得可能有用

【讨论】:

    【解决方案2】:

    我终于设法将每个场景添加到功能语句(第二个)中。只要我尝试将 NULL 甚至空字符串值添加到这些值,它就会失败:

    $insertPostStmt->bindValue(':hotelId', ($category === 'hotel' ? $categoryId : "NULL"));
    $insertPostStmt->bindValue(':restoId', ($category === 'restaurant' ? $categoryId : "NULL"));
    $insertPostStmt->bindValue(':activityId', ($category === 'activity' ? $categoryId : "NULL"));
    

    这不是问题,因为它们是外键,默认为 NULL。我只需要就类别案例做出 3 种不同的陈述。

    希望对您有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2013-12-20
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      相关资源
      最近更新 更多