繁体   English   中英

PHP事务提交确定,但未更改mySql数据库

[英]PHP transaction commit OK but unchanged mySql database

我从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);

我很想找到关于它的最终结论。 感谢您的帮助

我终于设法找到将每个方案添加到功能性语句中的方法(第二个方案)。 只要我尝试将NULL甚至空字符串值添加到其中一个,这些操作都会失败:

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

没问题,因为它们是外键,因此默认情况下为NULL。 我只需要针对类别案例做出3种不同的陈述。

希望对您有帮助...

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

在pdo初始化之前添加以下行:

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

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

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

我认为可能有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM