{"id":3786,"date":"2025-06-28T12:59:39","date_gmt":"2025-06-28T04:59:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3786"},"modified":"2025-06-28T12:59:39","modified_gmt":"2025-06-28T04:59:39","slug":"%e8%ae%a2%e5%8d%95%e4%b8%8b%e5%8d%95-%e5%ba%93%e5%ad%98%e9%94%81%e5%ae%9a%e7%9a%84%e5%ae%8c%e6%95%b4%e4%ba%8b%e5%8a%a1%e5%a4%84%e7%90%86%e9%80%bb%e8%be%91","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/06\/28\/%e8%ae%a2%e5%8d%95%e4%b8%8b%e5%8d%95-%e5%ba%93%e5%ad%98%e9%94%81%e5%ae%9a%e7%9a%84%e5%ae%8c%e6%95%b4%e4%ba%8b%e5%8a%a1%e5%a4%84%e7%90%86%e9%80%bb%e8%be%91\/","title":{"rendered":"\u201c\u8ba2\u5355\u4e0b\u5355 + \u5e93\u5b58\u9501\u5b9a\u201d\u7684\u5b8c\u6574\u4e8b\u52a1\u5904\u7406\u903b\u8f91"},"content":{"rendered":"\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a<strong>\u5b8c\u6574\u5b9e\u6218\u6848\u4f8b<\/strong>\uff0c\u6f14\u793a\u5982\u4f55\u5728 <strong>PHP + MySQL\uff08\u4f7f\u7528 PDO\uff09<\/strong> \u4e2d\uff0c\u4f7f\u7528<strong>\u4e8b\u52a1\u673a\u5236\u786e\u4fdd\u8ba2\u5355\u4e0b\u5355\u4e0e\u5e93\u5b58\u9501\u5b9a\u7684\u539f\u5b50\u6027\u548c\u6570\u636e\u4e00\u81f4\u6027<\/strong>\u3002\u8fd9\u4e2a\u6a21\u5f0f\u9002\u7528\u4e8e\u5404\u79cd\u7535\u5546\u5e73\u53f0\u6216\u5e93\u5b58\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u6218\u76ee\u6807<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u6237\u4e0b\u5355<\/li>\n\n\n\n<li>\u7cfb\u7edf\u751f\u6210\u8ba2\u5355\u8bb0\u5f55<\/li>\n\n\n\n<li>\u68c0\u67e5\u5e93\u5b58\u662f\u5426\u5145\u8db3<\/li>\n\n\n\n<li>\u6263\u51cf\u5e93\u5b58\u6570\u91cf<\/li>\n\n\n\n<li>\u6240\u6709\u6b65\u9aa4\u5fc5\u987b\u4e00\u81f4\u6210\u529f\u6216\u5168\u90e8\u5931\u8d25<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u5e93\u7ed3\u6784\u793a\u4f8b\uff08MySQL\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5546\u54c1\u8868\nCREATE TABLE products (\n    id INT PRIMARY KEY AUTO_INCREMENT,\n    name VARCHAR(100),\n    stock INT NOT NULL\n) ENGINE=InnoDB;\n\n-- \u8ba2\u5355\u8868\nCREATE TABLE orders (\n    id INT PRIMARY KEY AUTO_INCREMENT,\n    product_id INT NOT NULL,\n    quantity INT NOT NULL,\n    created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n) ENGINE=InnoDB;<\/code><\/pre>\n\n\n\n<p>\u786e\u4fdd\u4f7f\u7528 InnoDB \u5f15\u64ce\uff0c\u5426\u5219\u4e8b\u52a1\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e0b\u5355\u903b\u8f91\u5206\u6790\uff08\u6838\u5fc3\u4e1a\u52a1\uff09<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f00\u542f\u4e8b\u52a1<\/li>\n\n\n\n<li>\u67e5\u8be2\u5f53\u524d\u5e93\u5b58<\/li>\n\n\n\n<li>\u5982\u679c\u5e93\u5b58\u5145\u8db3\uff0c\u63d2\u5165\u8ba2\u5355\u8bb0\u5f55<\/li>\n\n\n\n<li>\u66f4\u65b0\u5e93\u5b58\u6570\u91cf<\/li>\n\n\n\n<li>\u63d0\u4ea4\u4e8b\u52a1<\/li>\n\n\n\n<li>\u82e5\u4efb\u610f\u6b65\u9aa4\u5931\u8d25\uff0c\u5219\u56de\u6eda<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b8c\u6574 PHP \u4ee3\u7801\uff08\u4f7f\u7528 PDO + \u4e8b\u52a1\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"mysql:host=localhost;dbname=test\", \"root\", \"\", &#91;\n    PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION\n]);\n\n$productId = 1;  \/\/ \u5546\u54c1ID\n$buyQuantity = 2;  \/\/ \u8d2d\u4e70\u6570\u91cf\n\ntry {\n    \/\/ \u5f00\u542f\u4e8b\u52a1\n    $pdo-&gt;beginTransaction();\n\n    \/\/ \u67e5\u8be2\u5f53\u524d\u5e93\u5b58\n    $stmt = $pdo-&gt;prepare(\"SELECT stock FROM products WHERE id = :id FOR UPDATE\");\n    $stmt-&gt;execute(&#91;':id' =&gt; $productId]);\n    $product = $stmt-&gt;fetch(PDO::FETCH_ASSOC);\n\n    if (!$product) {\n        throw new Exception(\"\u5546\u54c1\u4e0d\u5b58\u5728\");\n    }\n\n    if ($product&#91;'stock'] &lt; $buyQuantity) {\n        throw new Exception(\"\u5e93\u5b58\u4e0d\u8db3\");\n    }\n\n    \/\/ \u63d2\u5165\u8ba2\u5355\n    $insertOrder = $pdo-&gt;prepare(\"INSERT INTO orders (product_id, quantity) VALUES (:pid, :qty)\");\n    $insertOrder-&gt;execute(&#91;':pid' =&gt; $productId, ':qty' =&gt; $buyQuantity]);\n\n    \/\/ \u66f4\u65b0\u5e93\u5b58\n    $updateStock = $pdo-&gt;prepare(\"UPDATE products SET stock = stock - :qty WHERE id = :pid\");\n    $updateStock-&gt;execute(&#91;':qty' =&gt; $buyQuantity, ':pid' =&gt; $productId]);\n\n    \/\/ \u63d0\u4ea4\u4e8b\u52a1\n    $pdo-&gt;commit();\n\n    echo \"\u2705 \u4e0b\u5355\u6210\u529f\";\n\n} catch (Exception $e) {\n    $pdo-&gt;rollBack();\n    echo \"\u274c \u4e0b\u5355\u5931\u8d25\uff1a\" . $e-&gt;getMessage();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d4b\u8bd5\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u521d\u59cb\u5316\u5546\u54c1\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO products (name, stock) VALUES ('\u624b\u673a', 10);<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u591a\u6b21\u8bbf\u95ee\u4e0b\u5355\u4ee3\u7801\uff0c\u6a21\u62df\u5e93\u5b58\u6263\u51cf\u3002\u5e93\u5b58\u4e0d\u8db3\u65f6\u4f1a\u81ea\u52a8\u5931\u8d25\u5e76\u56de\u6eda\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b89\u5168\u6027\u4e0e\u5e76\u53d1\u8bf4\u660e<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u4e86 <code>SELECT ... FOR UPDATE<\/code> \u6765<strong>\u9501\u5b9a\u884c\u7ea7\u8bb0\u5f55<\/strong>\uff0c\u907f\u514d\u5e76\u53d1\u4e0b\u591a\u7528\u6237\u540c\u65f6\u6263\u51cf\u5bfc\u81f4\u8d85\u5356<\/li>\n\n\n\n<li>\u6240\u6709\u903b\u8f91\u90fd\u5728\u4e8b\u52a1\u4e2d\u5b8c\u6210\uff0c<strong>\u4fdd\u8bc1\u4e86\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u4e0e\u4e00\u81f4\u6027<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u6269\u5c55\u6027\u5efa\u8bae<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6a21\u5757<\/th><th>\u5efa\u8bae<\/th><\/tr><\/thead><tbody><tr><td>\u591a\u5546\u54c1\u4e0b\u5355<\/td><td>\u6269\u5c55 orders \u8868\u7ed3\u6784 + orders_items \u5b50\u8868<\/td><\/tr><tr><td>\u5206\u5e03\u5f0f\u67b6\u6784<\/td><td>\u8003\u8651\u4f7f\u7528\u5206\u5e03\u5f0f\u4e8b\u52a1\u6216 TCC\/Saga \u6a21\u5f0f<\/td><\/tr><tr><td>\u9ad8\u5e76\u53d1\u4f18\u5316<\/td><td>\u589e\u52a0\u5e93\u5b58\u7f13\u5b58\u3001\u6d88\u606f\u961f\u5217\u5f02\u6b65\u5904\u7406<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 \u5b98\u65b9\u53c2\u8003\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/pdo.begintransaction.php\">PDO \u4e8b\u52a1\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/innodb-transaction-model.html\">MySQL InnoDB \u6587\u6863\uff1a\u4e8b\u52a1\u4e0e\u9501<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/owasp.org\/www-project-top-ten\/\">OWASP \u6570\u636e\u5b8c\u6574\u6027\u4fdd\u62a4\u6307\u5357<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u5b9e\u6218\u6848\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5728 PHP + MySQL\uff08\u4f7f\u7528 PDO\uff09 \u4e2d\uff0c\u4f7f\u7528\u4e8b\u52a1\u673a\u5236\u786e\u4fdd\u8ba2\u5355\u4e0b\u5355\u4e0e\u5e93\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3787,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-3786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3786"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3786\/revisions"}],"predecessor-version":[{"id":3788,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3786\/revisions\/3788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3787"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}