{"id":142,"date":"2021-03-08T00:29:23","date_gmt":"2021-03-08T00:29:23","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=142"},"modified":"2021-07-07T07:37:02","modified_gmt":"2021-07-07T07:37:02","slug":"php-pdo-insert","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-pdo\/php-pdo-insert\/","title":{"rendered":"Inserting Data into Tables from PDO"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to insert one or more rows into a table using PHP PDO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-steps-for-inserting-a-row-into-a-table'>The steps for inserting a row into a table <a href=\"#the-steps-for-inserting-a-row-into-a-table\" class=\"anchor\" id=\"the-steps-for-inserting-a-row-into-a-table\" title=\"Anchor for The steps for inserting a row into a table\">#<\/a><\/h2>\n\n\n\n<p>To insert a row into a table, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-mysql\/\">connect to the database<\/a> by creating a new <code>PDO<\/code> object.<\/li><li>Second, construct the <code>INSERT<\/code> statement. If you need to pass a value to the <code>INSERT<\/code> statement, you can use the placeholders in the format <code>:parameter<\/code>. Later, you can substitute the <code>parameter<\/code> by its value.<\/li><li>Third, create a <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-prepared-statement\/\">prepared statement<\/a> by calling the <code>prepare()<\/code> method of the PDO object. The <code>prepare()<\/code> method returns an instance of the <code>PDOStatement<\/code> class.<\/li><li>Finally, call the <code>execute()<\/code> method of the prepared statement and pass the values.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='inserting-a-row-into-a-table-example'>Inserting a row into a table example <a href=\"#inserting-a-row-into-a-table-example\" class=\"anchor\" id=\"inserting-a-row-into-a-table-example\" title=\"Anchor for Inserting a row into a table example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to insert a new row into the <code>publishers<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pdo = <span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ insert a single publisher<\/span>\n$name = <span class=\"hljs-string\">'Macmillan'<\/span>;\n$sql = <span class=\"hljs-string\">'INSERT INTO publishers(name) VALUES(:name)'<\/span>;\n\n$statement = $pdo-&gt;prepare($sql);\n\n$statement-&gt;execute(&#91;\n\t<span class=\"hljs-string\">':name'<\/span> =&gt; $name\n]);\n\n$publisher_id = $pdo-&gt;lastInsertId();\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The publisher id '<\/span> . $publisher_id . <span class=\"hljs-string\">' was inserted'<\/span>;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, use the <code>connect.php<\/code> to connect to the <code>bookldb<\/code> database.<\/li><li>Next, construct an SQL <code>INSERT<\/code> statement. The <code>:name<\/code> is the placeholder that will be replaced by a publisher name.<\/li><li>Then, prepare the <code>INSERT<\/code> statement for execution by calling the <code>prepare()<\/code> method of the <code>$pdo<\/code> instance. The prepare() method returns an instance of the <code>PDOStatement<\/code> class.<\/li><li>After that, execute the prepared statement by passing the values to the <code>execute()<\/code> method.<\/li><li>Finally, get the inserted id by calling the <code>lastInsertId()<\/code> method of the PDO object.<\/li><\/ul>\n\n\n\n<p class=\"note\">Note that this tutorial uses the <code>connect.php<\/code> script developed in the <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-mysql\/\">connecting to the database<\/a> tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='inserting-multiple-rows-into-a-table-example'>Inserting multiple rows into a table example <a href=\"#inserting-multiple-rows-into-a-table-example\" class=\"anchor\" id=\"inserting-multiple-rows-into-a-table-example\" title=\"Anchor for Inserting multiple rows into a table example\">#<\/a><\/h2>\n\n\n\n<p>To insert multiple rows into a table, you need to call <code>execute()<\/code> the method multiple times. The method inserts a new row into the table in each call. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pdo = <span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\n\n$names = &#91;\n\t<span class=\"hljs-string\">'Penguin\/Random House'<\/span>,\n\t<span class=\"hljs-string\">'Hachette Book Group'<\/span>,\n\t<span class=\"hljs-string\">'Harper Collins'<\/span>,\n\t<span class=\"hljs-string\">'Simon and Schuster'<\/span>\n];\n\n$sql = <span class=\"hljs-string\">'INSERT INTO publishers(name) VALUES(:name)'<\/span>;\n\n$statement = $pdo-&gt;prepare($sql);\n\n<span class=\"hljs-keyword\">foreach<\/span> ($names <span class=\"hljs-keyword\">as<\/span> $name) {\n\t$statement-&gt;execute(&#91;\n\t\t<span class=\"hljs-string\">':name'<\/span> =&gt; $name\n\t]);\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we have a list of publishers stored in the <code>$names<\/code> array. <\/p>\n\n\n\n<p>To insert these publishers into the <code>publishers<\/code> table, we iterate over the elements of the <code>$names<\/code> array using the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-foreach\/\">foreach<\/a><\/code> and insert each element into the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use a prepared statement to insert one or more rows into a table.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"142\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/php-pdo-insert\/\"\n\t\t\t\tdata-post-title=\"Inserting Data into Tables from PDO\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"142\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/php-pdo-insert\/\"\n\t\t\t\tdata-post-title=\"Inserting Data into Tables from PDO\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to insert one or more rows into a table using PHP PDO. The steps for inserting a row into a table # To insert a row into a table, you follow these steps: First, connect to the database by creating a new PDO object. Second, construct the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-142","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/142\/revisions"}],"predecessor-version":[{"id":2134,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/142\/revisions\/2134"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}