{"id":3139,"date":"2025-03-22T10:48:33","date_gmt":"2025-03-22T02:48:33","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3139"},"modified":"2025-03-22T10:48:33","modified_gmt":"2025-03-22T02:48:33","slug":"php-ajax-%e4%b8%8e-mysql","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-ajax-%e4%b8%8e-mysql\/","title":{"rendered":"PHP &#8211; AJAX \u4e0e MySQL"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. PHP &#8211; AJAX \u4e0e MySQL\uff1a\u7ed3\u5408\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p>\u5c06 <strong>PHP<\/strong>\u3001<strong>AJAX<\/strong> \u548c <strong>MySQL<\/strong> \u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u7684\u7f51\u9875\u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd9\u6837\u7528\u6237\u53ef\u4ee5\u901a\u8fc7 AJAX \u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0cPHP \u5904\u7406\u6570\u636e\uff0c\u5e76\u901a\u8fc7 MySQL \u6570\u636e\u5e93\u5b58\u50a8\u6216\u68c0\u7d22\u6570\u636e\uff0c\u6700\u7ec8\u66f4\u65b0\u9875\u9762\u800c\u65e0\u9700\u5237\u65b0\u3002\u5b83\u80fd\u663e\u8457\u63d0\u9ad8\u7f51\u9875\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5de5\u4f5c\u539f\u7406<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u4ea4\u4e92<\/strong>\uff1a\u7528\u6237\u901a\u8fc7\u524d\u7aef\u754c\u9762\u89e6\u53d1\u67d0\u4e9b\u52a8\u4f5c\uff08\u4f8b\u5982\u70b9\u51fb\u6309\u94ae\u3001\u63d0\u4ea4\u8868\u5355\u7b49\uff09\uff0c\u8fd9\u65f6 JavaScript\uff08AJAX\uff09\u4f1a\u53d1\u9001\u4e00\u4e2a\u5f02\u6b65\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u3002<\/li>\n\n\n\n<li><strong>PHP \u5904\u7406\u8bf7\u6c42<\/strong>\uff1aPHP \u63a5\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u5904\u7406\u903b\u8f91\uff0c\u4f8b\u5982\u4e0e MySQL \u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u6267\u884c\u67e5\u8be2\u6216\u63d2\u5165\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>MySQL \u67e5\u8be2\u6216\u64cd\u4f5c<\/strong>\uff1aPHP \u4f1a\u901a\u8fc7 MySQL \u67e5\u8be2\u6570\u636e\u5e93\uff08\u4f8b\u5982\u83b7\u53d6\u6570\u636e\u3001\u63d2\u5165\u6570\u636e\u7b49\uff09\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u54cd\u5e94<\/strong>\uff1aPHP \u5c06\u5904\u7406\u7ed3\u679c\uff08\u5982\u67e5\u8be2\u7ed3\u679c\u3001\u64cd\u4f5c\u53cd\u9988\uff09\u8fd4\u56de\u7ed9\u524d\u7aef\uff0cAJAX \u83b7\u53d6\u54cd\u5e94\u5e76\u66f4\u65b0\u7f51\u9875\u7684\u67d0\u4e2a\u90e8\u5206\uff0c\u800c\u4e0d\u5237\u65b0\u6574\u4e2a\u9875\u9762\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u793a\u4f8b\uff1aAJAX \u4e0e PHP \u4e0e MySQL \u7ed3\u5408\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 AJAX\u3001PHP \u548c MySQL \u5b9e\u73b0\u7684\u4f8b\u5b50\uff0c\u901a\u8fc7 AJAX \u5411 PHP \u53d1\u9001\u8bf7\u6c42\uff0cPHP \u67e5\u8be2 MySQL \u6570\u636e\u5e93\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u6700\u7ec8\u66f4\u65b0\u7f51\u9875\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1. \u6570\u636e\u5e93\u8bbe\u7f6e<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u5047\u8bbe\u4f60\u5df2\u7ecf\u5728 MySQL \u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u636e\u5e93\u548c\u4e00\u4e2a\u8868\u3002\u4ee5\u4e0b\u662f\u6570\u636e\u5e93\u521b\u5efa\u548c\u8868\u7ed3\u6784\u7684 SQL \u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u521b\u5efa\u6570\u636e\u5e93\nCREATE DATABASE ajax_example;\n\n-- \u9009\u62e9\u6570\u636e\u5e93\nUSE ajax_example;\n\n-- \u521b\u5efa\u7528\u6237\u4fe1\u606f\u8868\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL,\n    email VARCHAR(100) NOT NULL\n);\n\n-- \u63d2\u5165\u4e00\u4e9b\u6d4b\u8bd5\u6570\u636e\nINSERT INTO users (username, email) VALUES ('Alice', 'alice@example.com');\nINSERT INTO users (username, email) VALUES ('Bob', 'bob@example.com');\nINSERT INTO users (username, email) VALUES ('Charlie', 'charlie@example.com');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2. HTML \u9875\u9762\uff1aAJAX \u8bf7\u6c42\u89e6\u53d1<\/strong><\/h3>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684 HTML \u9875\u9762\uff0c\u5b83\u901a\u8fc7\u70b9\u51fb\u6309\u94ae\u53d1\u9001 AJAX \u8bf7\u6c42\u6765\u4ece MySQL \u83b7\u53d6\u7528\u6237\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;PHP - AJAX \u4e0e MySQL \u793a\u4f8b&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;h1&gt;\u7528\u6237\u5217\u8868&lt;\/h1&gt;\n    &lt;button id=\"loadUsersButton\"&gt;\u52a0\u8f7d\u7528\u6237&lt;\/button&gt;\n    &lt;div id=\"userList\"&gt;&lt;\/div&gt;\n\n    &lt;script&gt;\n        \/\/ JavaScript\uff1a\u76d1\u542c\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u53d1\u9001 AJAX \u8bf7\u6c42\n        document.getElementById('loadUsersButton').addEventListener('click', function() {\n            var xhr = new XMLHttpRequest();\n            xhr.open('GET', 'fetch_users.php', true); \/\/ \u5411 PHP \u53d1\u9001 GET \u8bf7\u6c42\n            xhr.onreadystatechange = function() {\n                if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {\n                    \/\/ \u83b7\u53d6\u670d\u52a1\u5668\u8fd4\u56de\u7684\u7528\u6237\u6570\u636e\u5e76\u663e\u793a\u5728\u9875\u9762\u4e0a\n                    document.getElementById('userList').innerHTML = xhr.responseText;\n                }\n            };\n            xhr.send(); \/\/ \u53d1\u9001\u8bf7\u6c42\n        });\n    &lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3. PHP \u6587\u4ef6\uff08fetch_users.php\uff09\uff1a\u67e5\u8be2 MySQL \u6570\u636e\u5e93<\/strong><\/h3>\n\n\n\n<p><code>fetch_users.php<\/code> \u6587\u4ef6\u7528\u6765\u63a5\u6536 AJAX \u8bf7\u6c42\uff0c\u5e76\u4ece MySQL \u67e5\u8be2\u7528\u6237\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"ajax_example\";\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u6267\u884c\u67e5\u8be2\n$sql = \"SELECT id, username, email FROM users\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f93\u51fa\u6bcf\u884c\u6570\u636e\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"&lt;p&gt;User ID: \" . $row&#91;\"id\"] . \" - Name: \" . $row&#91;\"username\"] . \" - Email: \" . $row&#91;\"email\"] . \"&lt;\/p&gt;\";\n    }\n} else {\n    echo \"\u6ca1\u6709\u627e\u5230\u7528\u6237\u6570\u636e\";\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4. \u5de5\u4f5c\u6d41\u7a0b<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7528\u6237\u70b9\u51fb\u201c\u52a0\u8f7d\u7528\u6237\u201d\u6309\u94ae\uff0c\u89e6\u53d1 JavaScript \u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li>JavaScript \u901a\u8fc7 <code>XMLHttpRequest<\/code> \u5411\u670d\u52a1\u5668\u53d1\u9001 AJAX \u8bf7\u6c42\uff0c\u8bf7\u6c42 <code>fetch_users.php<\/code>\u3002<\/li>\n\n\n\n<li><code>fetch_users.php<\/code> \u8fde\u63a5\u5230 MySQL \u6570\u636e\u5e93\uff0c\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\uff0c\u8fd4\u56de\u7528\u6237\u5217\u8868\u6570\u636e\u3002<\/li>\n\n\n\n<li>AJAX \u63a5\u6536\u5230\u6765\u81ea PHP \u7684\u54cd\u5e94\uff0c\u5e76\u5c06\u5176\u63d2\u5165\u5230\u7f51\u9875\u7684 <code>#userList<\/code> \u5143\u7d20\u4e2d\uff0c\u66f4\u65b0\u9875\u9762\u5185\u5bb9\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5b9e\u73b0\u6570\u636e\u63d2\u5165\u529f\u80fd\uff1a\u901a\u8fc7 AJAX \u5411 MySQL \u63d2\u5165\u6570\u636e<\/strong><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u901a\u8fc7 AJAX \u5411 PHP \u53d1\u9001\u6570\u636e\u63d2\u5165 MySQL \u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1. HTML \u9875\u9762\uff1a\u63d2\u5165\u7528\u6237\u4fe1\u606f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;\u63d2\u5165\u7528\u6237\u4fe1\u606f&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;h1&gt;\u63d2\u5165\u7528\u6237\u4fe1\u606f&lt;\/h1&gt;\n    &lt;form id=\"addUserForm\"&gt;\n        &lt;label for=\"username\"&gt;\u7528\u6237\u540d\uff1a&lt;\/label&gt;\n        &lt;input type=\"text\" id=\"username\" name=\"username\" required&gt;\n        &lt;br&gt;\n        &lt;label for=\"email\"&gt;\u7535\u5b50\u90ae\u4ef6\uff1a&lt;\/label&gt;\n        &lt;input type=\"email\" id=\"email\" name=\"email\" required&gt;\n        &lt;br&gt;\n        &lt;button type=\"submit\"&gt;\u6dfb\u52a0\u7528\u6237&lt;\/button&gt;\n    &lt;\/form&gt;\n\n    &lt;div id=\"responseMessage\"&gt;&lt;\/div&gt;\n\n    &lt;script&gt;\n        document.getElementById('addUserForm').addEventListener('submit', function(event) {\n            event.preventDefault(); \/\/ \u963b\u6b62\u8868\u5355\u7684\u9ed8\u8ba4\u63d0\u4ea4\u884c\u4e3a\n\n            var formData = new FormData(this); \/\/ \u83b7\u53d6\u8868\u5355\u6570\u636e\n\n            var xhr = new XMLHttpRequest();\n            xhr.open('POST', 'add_user.php', true); \/\/ \u5411 PHP \u53d1\u9001 POST \u8bf7\u6c42\n\n            \/\/ \u76d1\u542c\u8bf7\u6c42\u5b8c\u6210\u540e\u7684\u5904\u7406\n            xhr.onload = function() {\n                if (xhr.status === 200) {\n                    document.getElementById('responseMessage').innerHTML = xhr.responseText; \/\/ \u663e\u793a\u670d\u52a1\u5668\u54cd\u5e94\n                } else {\n                    document.getElementById('responseMessage').innerHTML = '\u63d0\u4ea4\u5931\u8d25\uff0c\u8bf7\u91cd\u8bd5\u3002';\n                }\n            };\n\n            \/\/ \u53d1\u9001\u8868\u5355\u6570\u636e\n            xhr.send(formData);\n        });\n    &lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2. PHP \u6587\u4ef6\uff08add_user.php\uff09\uff1a\u63d2\u5165\u6570\u636e\u5230 MySQL<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"ajax_example\";\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u83b7\u53d6\u8868\u5355\u6570\u636e\n$username = $_POST&#91;'username'];\n$email = $_POST&#91;'email'];\n\n\/\/ \u63d2\u5165\u6570\u636e\u5230\u6570\u636e\u5e93\n$sql = \"INSERT INTO users (username, email) VALUES ('$username', '$email')\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"\u65b0\u7528\u6237\u6dfb\u52a0\u6210\u529f\uff01\";\n} else {\n    echo \"\u9519\u8bef: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3. \u5de5\u4f5c\u6d41\u7a0b<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7528\u6237\u586b\u5199\u8868\u5355\u5e76\u70b9\u51fb\u201c\u6dfb\u52a0\u7528\u6237\u201d\u6309\u94ae\uff0c\u89e6\u53d1 JavaScript \u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li>JavaScript \u901a\u8fc7 AJAX \u5c06\u8868\u5355\u6570\u636e\u53d1\u9001\u5230 <code>add_user.php<\/code>\u3002<\/li>\n\n\n\n<li><code>add_user.php<\/code> \u5c06\u6570\u636e\u63d2\u5165\u5230 MySQL \u6570\u636e\u5e93\uff0c\u5e76\u8fd4\u56de\u64cd\u4f5c\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>JavaScript \u83b7\u53d6\u670d\u52a1\u5668\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u5e76\u66f4\u65b0\u9875\u9762\u4e0a\u7684 <code>#responseMessage<\/code> \u5143\u7d20\uff0c\u63d0\u793a\u7528\u6237\u662f\u5426\u6dfb\u52a0\u6210\u529f\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5c06 <strong>PHP<\/strong>\u3001<strong>AJAX<\/strong> \u548c <strong>MySQL<\/strong> \u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u7684\u3001\u65e0\u9700\u5237\u65b0\u9875\u9762\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u901a\u8fc7 AJAX\uff0c\u524d\u7aef\u53ef\u4ee5\u4e0e\u540e\u7aef PHP \u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u800c PHP \u901a\u8fc7 MySQL \u8bbf\u95ee\u548c\u5904\u7406\u6570\u636e\u5e93\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u5b9e\u65f6\u3001\u65e0\u5237\u65b0\u7684\u6570\u636e\u66f4\u65b0\u548c\u63d2\u5165\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. PHP &#8211; AJAX \u4e0e MySQL\uff1a\u7ed3\u5408\u4f7f\u7528 \u5c06 PHP\u3001AJAX \u548c MySQL \u7ed3\u5408\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3139","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\/3139","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=3139"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3139\/revisions"}],"predecessor-version":[{"id":3141,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3139\/revisions\/3141"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3140"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}