dpzbh1779 2015-09-27 23:51
浏览 75
已采纳

使用回滚在事务内部进行SQL查询插入检查

In my shop system I'm using this code to insert in the DB customer order details and the products that belongs to that o:

$connection->beginTransaction();
try
{
    $sql = "INSERT INTO orders (customer_id, order_price, order_date, order_hour)
            VALUES (?, ?, ?, ?)";

    $query = $connection->prepare($sql);
    $query->execute(array
    (
        $user['user_id'],
        $order_price,
        $date,
        $hour
    ));

    if($query)
    {
        $id_of_respective_order = $connection->lastInsertId();

        $sql = "INSERT INTO purchased_products (order_id, product_name, product_price, quantity, extras)
                VALUES (?, ?, ?, ?, ?)";

        $query = $connection->prepare($sql);

        foreach($_SESSION['cart'] as $product)
        {
            $extras = null;
            $product_price = $product['product_price'] * $product['quantity'];

            if($product['extras'] != NULL)
            {
                foreach($product['extras'] as $extra)
                {
                    $extras .= $extra['extra_quantity'] ."x". $extra['extra_name'] ."<br/>";
                    $product_price += $extra['extra_total'] * $product['quantity'];
                }
            }

            $query->execute(array
            (
                $id_of_respective_order,
                $product['product_name'],
                $product_price,
                $product['quantity'],
                $extras
            ));
        }

        unset($_SESSION['cart']);

        echo "<script>alert('Your purchase was completed!');
        window.location = '/my-orders.php';
        </script>";
    }
    else
    {
        echo "<script>alert('An error ocurred while completing your purchase. Please try again!');
        window.location = '/my-cart.php';</script>";
    }
    $connection->commit();
}
catch(PDOException $exception) 
{
    $connection->rollBack();
    echo "<script>alert('An error ocurred while completing your purchase. Please try again!');
    window.location = '/my-cart.php';</script>";
}

My question is in regards to code optimization for error checking. It is recommended that I use if ($query) even with the catch and rollBack as I'm doing? It is necessary or I can use only catch and rollBack because it will check for erros by itself?

  • 写回答

1条回答 默认 最新

  • doukengzi3517 2015-09-28 00:42
    关注

    You don't need to use if if you have your PDO error set to throwing the exception.

    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集