PHP事务提交OK但未更改的mySql数据库

时间:2016-01-17 22:36:04

标签: php mysql pdo insert commit

我遇到了从php脚本在mySql数据库中插入记录的问题。这是我的代码:

// 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());
}

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

我必须说我之前使用过这个声明,并且这个工作正常,所以db也可以:

$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);

我很想知道关于它的最后一句话。谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

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

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

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

希望它会有所帮助...

答案 1 :(得分:0)

您可能无法正确捕获错误。您必须确保PDO报告级别正常。

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

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

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

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

我认为它可能有用

相关问题