$dbh->beginTransaction();
$content = "數據表中已經有相同的內容了。";
if (!empty($content)) {
$sql = "SELECT * FROM Contents WHERE Content = ?";
$stmt = $dbh->prepare($sql);
$stmt->execute(array($content));
/*$stmt->bindValue(1, $content);
$stmt->execute();*/
if ($stmt->rowCount()){
echo "數據表中已經有相同的內容了。";
} else {
$sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(":content", $content);
$successful = $stmt->execute();
$dbh->rollBack();
echo "rollback";
/*if ($successful) {
$num = $stmt->rowCount();
echo "插入了{$num}條記錄。";
//echo $stmt->queryString;
} else {
echo "插入記錄失敗!";
}*/
}
} else {
echo "沒有內容";
}