{"id":48105,"date":"2024-12-02T14:45:12","date_gmt":"2024-12-02T06:45:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48105\/"},"modified":"2024-12-02T14:45:12","modified_gmt":"2024-12-02T06:45:12","slug":"php-%e5%8f%82%e6%95%b0%e7%bb%91%e5%ae%9a%e4%b8%8e-sql-%e6%b3%a8%e5%85%a5%e7%9a%84%e5%85%b3%e8%81%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48105\/","title":{"rendered":"PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729989311671d8abff2539.jpg\" class=\"aligncenter\" title=\"PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054\u63d2\u56fe\" alt=\"PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054<\/h2>\n<p><strong>\u4ec0\u4e48\u662f\u53c2\u6570\u7ed1\u5b9a\uff1f<\/strong><\/p>\n<p>\u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5b89\u5168\u5b9e\u8df5\uff0c\u901a\u8fc7\u5360\u4f4d\u7b26\u5c06\u5916\u90e8\u6570\u636e\u4f20\u9001\u5230 SQL \u67e5\u8be2\u4e2d\u3002\u5b83\u53ef\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u540e\u8005\u662f\u653b\u51fb\u8005\u901a\u8fc7\u672a\u7ecf\u9a8c\u8bc1\u7684\u6570\u636e\u6ce8\u5165\u6076\u610f\u4ee3\u7801\u5230\u6570\u636e\u5e93\u7684\u653b\u51fb\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 PHP mysqli \u6269\u5c55\u6267\u884c\u53c2\u6570\u7ed1\u5b9a\uff1a<\/p>\n<pre>$stmt = $mysqli-&gt;prepare(\"INSERT INTO users (username, password) VALUES (?, ?)\");\n$stmt-&gt;bind_param(\"ss\", $username, $password);\n$stmt-&gt;execute();<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li><code>prepare()<\/code> \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u9884\u5904\u7406\u8bed\u53e5\u3002<\/li>\n<li><code>bind_param()<\/code> \u65b9\u6cd5\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230\u5360\u4f4d\u7b26\u3002<\/li>\n<li><code>execute()<\/code> \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u8868\u5355\uff1a<\/p>\n<pre>&lt;form action=\"register.php\" method=\"post\"&gt;\n  &lt;label&gt;Username:&lt;\/label&gt;\n  &lt;input type=\"text\" name=\"username\"&gt;\n  &lt;label&gt;Password:&lt;\/label&gt;\n  &lt;input type=\"password\" name=\"password\"&gt;\n  &lt;input type=\"submit\" value=\"Register\"&gt;\n&lt;\/form&gt;<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u6765\u4fdd\u62a4\u6211\u4eec\u62b5\u5fa1 SQL \u6ce8\u5165\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u8fde\u63a5\u5230\u6570\u636e\u5e93\n$mysqli = new mysqli(\"localhost\", \"user\", \"password\", \"database\");\n\n\/\/ \u68c0\u67e5\u8868\u5355\u6570\u636e\n$username = $_POST['username'];\n$password = $_POST['password'];\n\n\/\/ \u51c6\u5907\u67e5\u8be2\n$stmt = $mysqli-&gt;prepare(\"INSERT INTO users (username, password) VALUES (?, ?)\");\n\n\/\/ \u7ed1\u5b9a\u53c2\u6570\n$stmt-&gt;bind_param(\"ss\", $username, $password);\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$stmt-&gt;execute();\n\n\/\/ \u5173\u95ed\u8bed\u53e5\u548c\u8fde\u63a5\n$stmt-&gt;close();\n$mysqli-&gt;close();\n?&gt;<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u662f\u9632\u5fa1 SQL \u6ce8\u5165\u653b\u51fb\u7684\u81f3\u5173\u91cd\u8981\u7684\u5b89\u5168\u5b9e\u8df5\u3002\u901a\u8fc7\u5360\u4f4d\u7b26\u4f20\u8f93\u6570\u636e\uff0c\u5b83\u6709\u52a9\u4e8e\u9632\u6b62\u653b\u51fb\u8005\u6ce8\u5165\u6076\u610f\u4ee3\u7801\u5e76\u7834\u574f\u4f60\u7684\u6570\u636e\u5e93\u3002\u901a\u8fc7\u5728\u4f60\u7684 PHP \u4ee3\u7801\u4e2d\u4f7f\u7528\u4e0a\u9762\u63d0\u4f9b\u7684\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u5728\u4fdd\u62a4\u4f60\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u514d\u53d7\u6b64\u7c7b\u653b\u51fb\u65b9\u9762\u8fc8\u51fa\u91cd\u8981\u4e00\u6b65\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054 \u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aPHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054\u300b\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e SQL \u6ce8\u5165\u7684\u5173\u8054 \u4ec0\u4e48\u662f\u53c2\u6570\u7ed1\u5b9a\uff1f \u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u6570\u636e\u5e93\u5b89\u5168\u5b9e\u8df5\uff0c\u901a\u8fc7\u5360\u4f4d\u7b26\u5c06\u5916\u90e8\u6570\u636e\u4f20\u9001\u5230 SQL \u67e5\u8be2\u4e2d\u3002\u5b83\u53ef\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u540e\u8005\u662f\u653b\u51fb\u8005\u901a\u8fc7\u672a\u7ecf\u9a8c\u8bc1\u7684\u6570\u636e\u6ce8\u5165\u6076\u610f\u4ee3\u7801\u5230\u6570\u636e\u5e93\u7684\u653b\u51fb\u7c7b\u578b\u3002 \u5982\u4f55\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\uff1f \u4f7f\u7528 PHP mysqli \u6269\u5c55\u6267\u884c\u53c2\u6570\u7ed1\u5b9a\uff1a $stmt = $mysqli-&gt;prepare(&#8220;INSERT INTO users (username, password) VALUES (?, ?)&#8221;); $stmt-&gt;bind_param(&#8220;ss&#8221;, $username, $password); $stmt-&gt;execute(); \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff1a prepare() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u9884\u5904\u7406\u8bed\u53e5\u3002 bind_param() \u65b9\u6cd5\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230\u5360\u4f4d\u7b26\u3002 execute() \u65b9\u6cd5\u6267\u884c\u67e5\u8be2\u3002 \u5b9e\u6218\u6848\u4f8b \u8003\u8651\u4ee5\u4e0b\u8868\u5355\uff1a &lt;form action=&#8221;register.php&#8221; method=&#8221;post&#8221;&gt; &lt;label&gt;Username:&lt;\/label&gt; &lt;input type=&#8221;text&#8221; name=&#8221;username&#8221;&gt; &lt;label&gt;Password:&lt;\/label&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48105","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48105"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48105\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}