{"id":5745,"date":"2019-02-04T06:05:57","date_gmt":"2019-02-04T06:05:57","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5745"},"modified":"2022-04-13T15:56:38","modified_gmt":"2022-04-13T15:56:38","slug":"how-to-use-pdo-to-insert-data-into-the-database","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-use-pdo-to-insert-data-into-the-database\/","title":{"rendered":"How to use PDO to insert data into the database?"},"content":{"rendered":"<p><a href=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/02\/How-to-use-PDO.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5763\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/02\/How-to-use-PDO.png\" alt=\"\" width=\"400\" height=\"215\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/02\/How-to-use-PDO.png 400w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/02\/How-to-use-PDO-300x161.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p><strong>The SQL code for the users table:<\/strong><\/p>\n<p>[code language=&#8221;sql&#8221;]<br \/>\nCREATE TABLE IF NOT EXISTS users (id int(11) NOT NULL AUTO_INCREMENT,<br \/>\nname varchar(60) DEFAULT NULL,<br \/>\nphone varchar(12) DEFAULT NULL,<br \/>\ncity varchar(60) DEFAULT NULL,<br \/>\ndate_added date DEFAULT NULL,<br \/>\nPRIMARY KEY (id)<br \/>\n)<br \/>\n[\/code]<\/p>\n<p><strong>1) Write a regular SQL query but, instead of values, put named placeholders. For example:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$sql = &quot;INSERT INTO `users`(`name`, `phone`, `city`, `date_added`)<br \/>\nVALUES(:name,:phone,:city,:date)&quot;;<br \/>\n[\/code]<\/p>\n<p>The use of placeholders is known as prepared statements. We use prepared statements as templates that we can fill later on with actual values.<br \/>\n<strong>2) Prepare the query:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$query = $dbh -&gt; prepare($sql);<br \/>\n[\/code]<\/p>\n<p><strong>3) Bind the placeholders to the variables:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$query-&gt;bindParam(&#8216;:name&#8217;,$name);<br \/>\n[\/code]<\/p>\n<p><strong>You can add a third parameter which filters the data before it reaches the database:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$query-&gt;bindParam(&#8216;:name&#8217;,$name,PDO::PARAM_STR);<br \/>\n$query-&gt;bindParam(&#8216;:phone&#8217;,$phone,PDO::PARAM_INT);<br \/>\n$query-&gt;bindParam(&#8216;:city&#8217;,$city,PDO::PARAM_STR);<br \/>\n$query-&gt;bindParam(&#8216;:date&#8217;,$date,PDO::PARAM_STR);<br \/>\n[\/code]<\/p>\n<ul>\n<li>PDO::PARAM_STR is used for strings.<\/li>\n<li>PDO::PARAM_INT is used for integers.<\/li>\n<li>PDO::PARAM_BOOL allows only boolean (true\/false) values.<\/li>\n<li>PDO::PARAM_NULL allows only NULL datatype.<\/li>\n<\/ul>\n<p><strong>4) Assign the values to the variables.<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$name = &quot;Amardeep Dubey&quot;;<br \/>\n$phone = &quot;1234567890&quot;;<br \/>\n$city = &quot;Bokaro&quot;;<br \/>\n$date = date(&#8216;Y-m-d&#8217;);<br \/>\n[\/code]<\/p>\n<p><strong>5) Execute the query:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$query -&gt; execute();<br \/>\n[\/code]<\/p>\n<p><strong>6) Check that the insertion really worked:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$lastInsertId = $dbh-&gt;lastInsertId();<br \/>\nif($lastInsertId&gt;0)<br \/>\n{<br \/>\necho &quot;OK&quot;;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\necho &quot;not OK&quot;;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>If the last inserted id is greater than zero, the insertion worked.<br \/>\n<strong>All code together now:<\/strong><\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\n$sql = &quot;INSERT INTO `users`<br \/>\n(`name`, `phone`, `city`, `date_added`)<br \/>\nVALUES<br \/>\n(:name,:phone,:city,:date)&quot;;<br \/>\n$query = $dbh -&gt; prepare($sql);<br \/>\n$query-&gt;bindParam(&#8216;:name&#8217;,$name,PDO::PARAM_STR);<br \/>\n$query-&gt;bindParam(&#8216;:phone&#8217;,$phone,PDO::PARAM_INT);<br \/>\n$query-&gt;bindParam(&#8216;:city&#8217;,$city,PDO::PARAM_STR);<br \/>\n$query-&gt;bindParam(&#8216;:date&#8217;,$date);<br \/>\n\/\/ Insert the first row<br \/>\n$name = &quot;Amardeep Dubey&quot;;<br \/>\n$phone = &quot;1234567890&quot;;<br \/>\n$city = &quot;Bokaro&quot;;<br \/>\n$date = date(&#8216;Y-m-d&#8217;);<br \/>\n$query -&gt; execute();<br \/>\n$lastInsertId = $dbh-&gt;lastInsertId();<br \/>\nif($lastInsertId&gt;0)<br \/>\n{<br \/>\necho &quot;OK&quot;;<br \/>\n }<br \/>\nelse {<br \/>\necho &quot;not OK&quot;; }<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SQL code for the users table: [code language=&#8221;sql&#8221;] CREATE TABLE IF NOT EXISTS users (id int(11) NOT NULL AUTO_INCREMENT, name varchar(60) DEFAULT NULL, phone varchar(12) DEFAULT NULL, city varchar(60)&#8230; <\/p>\n","protected":false},"author":1,"featured_media":5763,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5176,5177,5178],"class_list":["post-5745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-pdo","tag-php-data-objects","tag-php-pdo"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5745"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5745\/revisions"}],"predecessor-version":[{"id":5764,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5745\/revisions\/5764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/5763"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}