{"id":201,"date":"2023-04-18T16:32:03","date_gmt":"2023-04-18T13:32:03","guid":{"rendered":"http:\/\/newsitebuilder.ru\/?p=201"},"modified":"2023-04-18T16:32:03","modified_gmt":"2023-04-18T13:32:03","slug":"%d0%b3%d0%be%d1%81%d1%82%d0%b5%d0%b2%d0%b0%d1%8f-%d0%ba%d0%bd%d0%b8%d0%b3%d0%b0-%d0%bd%d0%b0-php","status":"publish","type":"post","link":"http:\/\/newsitebuilder.ru\/?p=201","title":{"rendered":"\u0413\u043e\u0441\u0442\u0435\u0432\u0430\u044f \u043a\u043d\u0438\u0433\u0430 \u043d\u0430 PHP"},"content":{"rendered":"<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0433\u043e\u0441\u0442\u0435\u0432\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u043d\u0430 PHP \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<!--more--><\/p>\n<p>1. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL:<\/p>\n<p>&#171;`sql<\/p>\n<p>CREATE TABLE guestbook (<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>id INT(11) NOT NULL AUTO_INCREMENT,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>name VARCHAR(50) NOT NULL,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>email VARCHAR(50) NOT NULL,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>message TEXT NOT NULL,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PRIMARY KEY (id)<\/p>\n<p>);<\/p>\n<p>&#171;`<\/p>\n<p>2. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0444\u0430\u0439\u043b\u044b index.php \u0438 process.php.<\/p>\n<p>3. \u0412 \u0444\u0430\u0439\u043b\u0435 index.php \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0444\u043e\u0440\u043c\u0443 \u0434\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f:<\/p>\n<p>&#171;`html<\/p>\n<p>&lt;form action=&#187;process.php&#187; method=&#187;post&#187;&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;label for=&#187;name&#187;&gt;Name:&lt;\/label&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;input type=&#187;text&#187; name=&#187;name&#187; id=&#187;name&#187; required&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;label for=&#187;email&#187;&gt;Email:&lt;\/label&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;input type=&#187;email&#187; name=&#187;email&#187; id=&#187;email&#187; required&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;label for=&#187;message&#187;&gt;Message:&lt;\/label&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;textarea name=&#187;message&#187; id=&#187;message&#187; required&gt;&lt;\/textarea&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;button type=&#187;submit&#187;&gt;Submit&lt;\/button&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&#171;`<\/p>\n<p>4. \u0412 \u0444\u0430\u0439\u043b\u0435 process.php \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0439\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u043e\u0440\u043c\u044b \u0438 \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0438\u0445 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>&#171;`php<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>$host = &#8216;localhost&#8217;;<\/p>\n<p>$user = &#8216;username&#8217;;<\/p>\n<p>$password = &#8216;password&#8217;;<\/p>\n<p>$dbname = &#8216;database_name&#8217;;<\/p>\n<p>$dsn = &#171;mysql:host=$host;dbname=$dbname;charset=utf8mb4&#187;;<\/p>\n<p>$options = [<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PDO::ATTR_EMULATE_PREPARES =&gt; false,<\/p>\n<p>];<\/p>\n<p>try {<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>$pdo = new PDO($dsn, $user, $password, $options);<\/p>\n<p>} catch (\\PDOException $e) {<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>throw new \\PDOException($e-&gt;getMessage(), (int)$e-&gt;getCode());<\/p>\n<p>}<\/p>\n<p>\/\/ \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0444\u043e\u0440\u043c\u044b<\/p>\n<p>$name = $_POST[&#8216;name&#8217;];<\/p>\n<p>$email = $_POST[&#8217;email&#8217;];<\/p>\n<p>$message = $_POST[&#8216;message&#8217;];<\/p>\n<p>\/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>$stmt = $pdo-&gt;prepare(&#171;INSERT INTO guestbook (name, email, message) VALUES (?, ?, ?)&#187;);<\/p>\n<p>$stmt-&gt;execute([$name, $email, $message]);<\/p>\n<p>\/\/ \u041f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443<\/p>\n<p>header(&#8216;Location: index.php&#8217;);<\/p>\n<p>exit;<\/p>\n<p>&#171;`<\/p>\n<p>5. \u0412 \u0444\u0430\u0439\u043b\u0435 index.php \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u0432\u0441\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<p>&#171;`php<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>$host = &#8216;localhost&#8217;;<\/p>\n<p>$user = &#8216;username&#8217;;<\/p>\n<p>$password = &#8216;password&#8217;;<\/p>\n<p>$dbname = &#8216;database_name&#8217;;<\/p>\n<p>$dsn = &#171;mysql:host=$host;dbname=$dbname;charset=utf8mb4&#187;;<\/p>\n<p>$options = [<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>PDO::ATTR_EMULATE_PREPARES =&gt; false,<\/p>\n<p>];<\/p>\n<p>try {<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>$pdo = new PDO($dsn, $user, $password, $options);<\/p>\n<p>} catch (\\PDOException $e) {<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>throw new \\PDOException($e-&gt;getMessage(), (int)$e-&gt;getCode());<\/p>\n<p>}<\/p>\n<p>\/\/ \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<p>$stmt = $pdo-&gt;query(&#171;SELECT * FROM guestbook ORDER BY created_at DESC&#187;);<\/p>\n<p>$messages = $stmt-&gt;fetchAll();<\/p>\n<p>?&gt;<\/p>\n<p>&lt;!&#8212; \u0412\u044b\u0432\u043e\u0434 \u0432\u0441\u0435\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 &#8212;&gt;<\/p>\n<p>&lt;?php foreach ($messages as $message): ?&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;div&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>&lt;h3&gt;&lt;?= $message[&#8216;name&#8217;] ?&gt;&lt;\/h3&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>&lt;p&gt;&lt;?= $message[&#8217;email&#8217;] ?&gt;&lt;\/p&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>&lt;p&gt;&lt;?= $message[&#8216;message&#8217;] ?&gt;&lt;\/p&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>&lt;p&gt;&lt;?= $message[&#8216;created_at&#8217;] ?&gt;&lt;\/p&gt;<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>&lt;\/div&gt;<\/p>\n<p>&lt;?php endforeach; ?&gt;<\/p>\n<p>&#171;`<\/p>\n<p>\u0413\u043e\u0441\u0442\u0435\u0432\u0430\u044f \u043a\u043d\u0438\u0433\u0430 \u043d\u0430 PHP \u0433\u043e\u0442\u043e\u0432\u0430!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0433\u043e\u0441\u0442\u0435\u0432\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u043d\u0430 PHP \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 MySQL \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n","protected":false},"author":1,"featured_media":202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[110,17],"tags":[111,4,112],"class_list":["post-201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","category-php","tag-mysql","tag-php","tag-112"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=201"}],"version-history":[{"count":1,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":203,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/posts\/201\/revisions\/203"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=\/wp\/v2\/media\/202"}],"wp:attachment":[{"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/newsitebuilder.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}