在交易期间,我可以在同一桌上使用删除查询两次吗?

时间:2014-05-27 10:45:36

标签: php mysql

我可以在交易期间两次在同一张桌子上使用删除查询吗? 我有两个数组想要在提交之前删除相同的表,如果一个失败希望可以使其回滚,但如果我使用下面的2删除查询一次有一些错误。我无法找到错误的信息......

  $connect_db_x_article->beginTransaction();

  // image
  if (!empty($data['gallery_old']['gallery_old_delete']['gallery_old_image_delete_id'])) {
    for ($i=0; $i < count($data['gallery_old']['gallery_old_delete']['gallery_old_image_delete_id']); $i++) {
      $file_id = $data['gallery_old']['gallery_old_delete']['gallery_old_image_delete_id'][$i];

      $sql = "SELECT * FROM article_gallery WHERE id = :id";
      $stmt = $connect_db_article->prepare($sql);
      $stmt->bindValue(':id', $file_id);
      $stmt->execute();
      $row = $stmt->fetchAll(PDO::FETCH_ASSOC);

      $sql = "DELETE FROM article_gallery WHERE id = :id";
      $stmt = $connect_db_article->prepare($sql);
      $stmt->bindValue(':id', $row[0]['id']);
      $stmt->execute();

      $path = $_SERVER['DOCUMENT_ROOT'].$base_uri.'/assets/images';
      $folder_path = $path.'/'.$id;
      $file_path = $folder_path.'/'.$row[0]['file_name'].'.'.$row[0]['file_format'];
      if (is_file($file_path)) {
        unlink($file_path);
      }
    }
  }

  // video_embed
  if (!empty($data['gallery_old']['gallery_old_delete']['gallery_old_video_embed_delete_id'])) {
    for ($i=0; $i < count($data['gallery_old']['gallery_old_delete']['gallery_old_video_embed_delete_id']); $i++) {
      $file_id = $data['gallery_old']['gallery_old_delete']['gallery_old_video_embed_delete_id'][$i];

      $sql = "DELETE FROM article_gallery WHERE id = :id";
      $stmt = $connect_db_article->prepare($sql);
      $stmt->bindValue(':id', $file_id);
      $stmt->execute();
    }
  }

  $connect_db_article->commit();

0 个答案:

没有答案