如何编写zend db事务

时间:2011-12-01 10:00:34

标签: php zend-db

我的代码有问题。当我试图执行它时没有显示任何错误,但它也没有从数据库中删除。

function delete(){
try {
require('/inc/connect.php');
$publicId = $_POST["publicId"];
$select = $db->select()
    ->from('test', '*')
    ->where('test_public_id = ?', $publicId);
$checkedPublicId = $db->fetchRow($select);
if(!$checkedPublicId){
    throw new Exception("No tour with that id was found");
}
$testId = $checkedPublicId['test_id'];
$db->beginTransaction();
$sql1 = $db -> query ("DELETE FROM folder_test WHERE test_id ='.$testId.'");
$sql2 = $db -> query ("DELETE FROM image WHERE test_id ='.$testId.'");
$sql3 = $db -> query ("DELETE FROM test_shares WHERE test_id ='.$testId.'");
$sql4 = $db -> query ("DELETE FROM test_stats WHERE test_public_id ='.$publicId.'");
$sql5 = $db -> query ("DELETE FROM slides WHERE test_id ='.$testId.'");
$sql6 = $db -> query ("DELETE FROM test WHERE test_public_id ='.$publicId.'");
$query = $db->commit();
}
catch(Exception $e) {
    $db->rollBack();
    throw new Exception($e);
}
}

1 个答案:

答案 0 :(得分:2)

您的查询已损坏。

$sql1 = $db -> query ('DELETE FROM folder_test WHERE test_id ='.$testId);
$sql2 = $db -> query ('DELETE FROM image WHERE test_id ='.$testId);
$sql3 = $db -> query ('DELETE FROM test_shares WHERE test_id ='.$testId);
$sql4 = $db -> query ('DELETE FROM test_stats WHERE test_public_id ='. $db -> quote($publicId));
$sql5 = $db -> query ('DELETE FROM slides WHERE test_id ='.$testId);
$sql6 = $db -> query ('DELETE FROM test WHERE test_public_id ='. $db -> quote($publicId));