{"id":28431,"date":"2024-11-24T09:22:12","date_gmt":"2024-11-24T01:22:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28431\/"},"modified":"2024-11-24T09:22:12","modified_gmt":"2024-11-24T01:22:12","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e5%b9%b6%e4%bb%a5-php-%e5%bd%a2%e5%bc%8f%e5%bd%a2%e6%88%90%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28431\/","title":{"rendered":"\u5982\u4f55\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4ee5 PHP \u5f62\u5f0f\u5f62\u6210\uff1f"},"content":{"rendered":"<blockquote><p>\n  \u8981\u4ece PHP \u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5c06\u5176\u663e\u793a\u5728\u8868\u5355\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1.\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a\u4f7f\u7528 MySQLi \u6216 PDO \u5efa\u7acb\u5230\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002 2.\u67e5\u8be2\u6570\u636e\u5e93\uff1a\u6267\u884cSQL\u67e5\u8be2\u4ee5\u68c0\u7d22\u6240\u9700\u7684\u6570\u636e\u3002 3.\u83b7\u53d6\u6570\u636e\uff1a\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u83b7\u53d6\u6570\u636e\u3002 4.\u586b\u5145\u8868\u5355\uff1a\u4f7f\u7528\u83b7\u53d6\u7684\u6570\u636e\u586b\u5199\u8868\u5355\u5b57\u6bb5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/080\/673e9dec93ba8232.png\" class=\"aligncenter\" title=\"\u5982\u4f55\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4ee5 PHP \u5f62\u5f0f\u5f62\u6210\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4ee5 PHP \u5f62\u5f0f\u5f62\u6210\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u7684\u7b80\u5355\u793a\u4f8bMySQLi:<\/p>\n<p><strong>\u7b2c 1 \u6b65\uff1a\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/p>\n<pre>&lt;?php \n$servername = \"localhost\"; \n$username = \"username\"; \n$password = \"password\"; \n$dbname = \"database_name\"; \n\n\/\/ Create connection \n$conn = new mysqli($servername, $username, $password, $dbname); \n\n\/\/ Check connection \nif ($conn-&gt;connect_error) {     \ndie(\"Connection failed: \" . $conn-&gt;connect_error); \n} \n?&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c 2 \u6b65\uff1a\u67e5\u8be2\u6570\u636e\u5e93<\/strong><\/p>\n<pre>&lt;?php $sql = \"SELECT id, name, email FROM users WHERE id = 1\"; \n\/\/ Example query \n$result = $conn-&gt;query($sql); \n?&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c 3 \u6b65\uff1a\u83b7\u53d6\u6570\u636e<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>&lt;?php $user = null; \nif ($result-&gt;num_rows &gt; 0) {     \n\/\/ Fetch associative array     \n$user = $result-&gt;fetch_assoc(); \n} else {     \necho \"No results found.\"; \n} \n?&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c 4 \u6b65\uff1a\u586b\u5199\u8868\u5355<\/strong><\/p>\n<pre>&lt;?php if ($user): ?&gt; \n&lt;form action=\"update.php\" method=\"post\"&gt; \n&lt;input type=\"hidden\" name=\"id\" value=\"&lt;?php echo $user['id']; ?&gt;\"&gt; \n&lt;label for=\"name\"&gt;Name:&lt;\/label&gt;     \n&lt;input type=\"text\" id=\"name\" name=\"name\" value=\"&lt;?php echo htmlspecialchars($user['name']); ?&gt;\"&gt; \n&lt;label for=\"email\"&gt;Email:&lt;\/label&gt;     \n&lt;input type=\"email\" id=\"email\" name=\"email\" value=\"&lt;?php echo htmlspecialchars($user['email']); ?&gt;\"&gt; \n&lt;input type=\"submit\" value=\"Update\"&gt; \n&lt;\/form&gt; \n&lt;?php endif; ?&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c 5 \u6b65\uff1a\u5173\u95ed\u8868\u5355\u8fde\u63a5<\/strong><\/p>\n<pre>&lt;?php \n$conn-&gt;close(); \n?&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8bf4\u660e\uff1a<\/p>\n<ul>\n  overflow-wrap: break-word;&#8221;&gt; <\/p>\n<li>\u6570\u636e\u5e93\u8fde\u63a5\uff1a\u5c06 localhost\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u79f0\u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645\u6570\u636e\u5e93\u51ed\u636e\u3002<\/li>\n<li>SQL \u67e5\u8be2\uff1a\u8c03\u6574 SQL \u67e5\u8be2\u4ee5\u83b7\u53d6\u60a8\u9700\u8981\u7684\u6570\u636e\uff08\u4f8b\u5982\uff0c\u901a\u8fc7\u66f4\u6539WHERE \u5b50\u53e5\uff09\u3002<\/li>\n<li>HTML \u8868\u5355\uff1a\u8868\u5355\u5b57\u6bb5\u7531\u83b7\u53d6\u7684\u6570\u636e\u586b\u5145\u3002\u663e\u793a\u7528\u6237\u8f93\u5165\u65f6\u4f7f\u7528 htmecialchars() \u9632\u6b62 XSS \u653b\u51fb\u3002<\/li>\n<li> \uff1a\u8868\u5355\u63d0\u4ea4\u5230 update.php\uff0c\u60a8\u5c06\u5728\u5176\u4e2d\u5904\u7406\u8868\u5355\u6570\u636e\u4ee5\u66f4\u65b0\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n<p>\u6b64\u793a\u4f8b\u4e3a\u60a8\u63d0\u4f9b\u4e86\u4f7f\u7528 PHP \u83b7\u53d6\u548c\u663e\u793a\u8868\u5355\u4e2d\u7684\u6570\u636e\u7684\u57fa\u672c\u7ed3\u6784\u3002\u6839\u636e\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u8981\u8c03\u6574 SQL \u67e5\u8be2\u548c\u8868\u5355\u5b57\u6bb5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u4ee5 PHP \u5f62\u5f0f\u5f62\u6210\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u4ece PHP \u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5c06\u5176\u663e\u793a\u5728\u8868\u5355\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1.\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a\u4f7f\u7528 MySQLi \u6216 PDO \u5efa\u7acb\u5230\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002 2.\u67e5\u8be2\u6570\u636e\u5e93\uff1a\u6267\u884cSQL\u67e5\u8be2\u4ee5\u68c0\u7d22\u6240\u9700\u7684\u6570\u636e\u3002 3.\u83b7\u53d6\u6570\u636e\uff1a\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u83b7\u53d6\u6570\u636e\u3002 4.\u586b\u5145\u8868\u5355\uff1a\u4f7f\u7528\u83b7\u53d6\u7684\u6570\u636e\u586b\u5199\u8868\u5355\u5b57\u6bb5\u3002 \u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u7684\u7b80\u5355\u793a\u4f8bMySQLi: \u7b2c 1 \u6b65\uff1a\u8fde\u63a5\u6570\u636e\u5e93 &lt;?php $servername = &#8220;localhost&#8221;; $username = &#8220;username&#8221;; $password = &#8220;password&#8221;; $dbname = &#8220;database_name&#8221;; \/\/ Create connection $conn = new mysqli($servername, $username, $password, $dbname); \/\/ Check connection if ($conn-&gt;connect_error) { die(&#8220;Connection failed: &#8221; . $conn-&gt;connect_error); } ?&gt; \u767b\u5f55\u540e\u590d\u5236 \u7b2c 2 \u6b65\uff1a\u67e5\u8be2\u6570\u636e\u5e93 [&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-28431","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28431","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=28431"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28431\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}