{"id":1444,"date":"2017-07-25T11:19:48","date_gmt":"2017-07-25T03:19:48","guid":{"rendered":"https:\/\/www.webteach.tw\/?p=1444"},"modified":"2017-07-25T11:19:48","modified_gmt":"2017-07-25T03:19:48","slug":"pdo-2","status":"publish","type":"post","link":"https:\/\/www.webteach.tw\/?p=1444","title":{"rendered":"[ PHP ] \u2013 #\u6559\u5b78- PDO INSERT \u57fa\u790e\u7bc4\u4f8b"},"content":{"rendered":"<p>\t\t\t\tPDO INSERT \u57fa\u790e\u7bc4\u4f8b<br \/>\n[php]<br \/>\n$code=uniqid();\/\/\u6703\u54e1\u7de8\u865f \u81ea\u52d5\u7522\u751f13\u78bc\u96a8\u6a5f\u78bc<br \/>\n$name=$_POST[&#8216;name&#8217;];\/\/\u6703\u54e1\u540d\u7a31<br \/>\n$account=$_POST[&#8216;account&#8217;];\/\/\u6703\u54e1\u5e33\u865f<br \/>\n$password=$_POST[&#8216;password&#8217;];\/\/\u6703\u54e1\u5bc6\u78bc<br \/>\n\/\/\u4ee5key=&gt;value \u5b58\u6b04\u4f4d\u540d\u7a31\u8207\u6b32\u5b58\u5165\u7684\u503c<br \/>\n$c=array(<br \/>\n&#8216;member_code&#8217;=&gt;&#8217;:code&#8217;,<br \/>\n&#8216;member_name&#8217;=&gt;&#8217;:name&#8217;,<br \/>\n&#8216;member_account&#8217;=&gt;&#8217;:account&#8217;,<br \/>\n&#8216;member_password&#8217;=&gt;&#8217;:password&#8217;<br \/>\n);<br \/>\n\/\/\u5229\u7528foreach\u8ff4\u5708 \u7d44\u6210\u6b04\u4f4d\u8207\u503c\u7684\u5b57\u4e32<br \/>\nforeach ($c as $key=&gt;$value){$a.=$key.&quot;,&quot;;$b.=$value.&quot;,&quot;;}<br \/>\n\/\/\u6b32\u57f7\u884c\u7684sql\u8a9e\u6cd5<br \/>\n$sth = $db-&gt;prepare(&quot;INSERT INTO member(&quot;.substr($a,0,-1).&quot;) VALUES(&quot;.substr($b,0,-1).&quot;)&quot;);<br \/>\n\/\/\u6b04\u4f4d\u8207\u76f8\u5c0d\u61c9\u8b8a\u6578<br \/>\n$sth-&gt;bindParam(&#8216;:code&#8217;,$code);<br \/>\n$sth-&gt;bindParam(&#8216;:name&#8217;,$name);<br \/>\n$sth-&gt;bindParam(&#8216;:account&#8217;,$account);<br \/>\n$sth-&gt;bindParam(&#8216;:password&#8217;,$password);<br \/>\n\/\/\u57f7\u884c<br \/>\n$stmt=$sth-&gt;execute();<br \/>\n\/\/\u82e5\u57f7\u884c\u6709\u932f\u8aa4\u8f38\u51fa\u932f\u8aa4\u8a0a\u606f\u4e26\u5c07\u7a0b\u5f0f\u4e2d\u65b7\u57f7\u884c<br \/>\nif (!$stmt) {print_r($sth-&gt;errorInfo());exit();}<br \/>\n[\/php]<br \/>\n<a href=\"https:\/\/www.webteach.tw\/?p=1434\" target=\"_blank\" rel=\"noopener noreferrer\">\u56dePHP\u6559\u5b78\u76ee\u9304<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PDO INSERT \u57fa\u790e\u7bc4\u4f8b [php] $co&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2201,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[444],"class_list":["post-1444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-pdo","hvn-theme-has-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/1444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1444"}],"version-history":[{"count":0,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/1444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/media\/2201"}],"wp:attachment":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}