{"id":375,"date":"2018-01-15T02:21:37","date_gmt":"2018-01-15T02:21:37","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=375"},"modified":"2018-01-15T02:21:37","modified_gmt":"2018-01-15T02:21:37","slug":"php-insert-data-into-mysql","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-insert-data-into-mysql\/","title":{"rendered":"PHP Insert Data Into MySQL"},"content":{"rendered":"<p>Once you are done with creating the table in the MySQL database. The next step in the process is adding value to the database.<\/p>\n<p>You can add new value by using INSERT INTO MySQL statement to add a new record in the database.<\/p>\n<p><strong>Example Code:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$servername = &quot;localhost&quot;;<br \/>\n$username = &quot;username&quot;;<br \/>\n$password = &quot;password&quot;;<br \/>\n$dbname = &quot;myDB&quot;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\n\/\/ Check connection<br \/>\nif ($conn-&gt;connect_error) {<br \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n} <\/p>\n<p>$sql = &quot;INSERT INTO MyGuests (firstname, lastname, email)<br \/>\nVALUES (&#8216;John&#8217;, &#8216;Doe&#8217;, &#8216;john@example.com&#8217;)&quot;;<\/p>\n<p>if ($conn-&gt;query($sql) === TRUE) {<br \/>\n    echo &quot;New record created successfully&quot;;<br \/>\n} else {<br \/>\n    echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . $conn-&gt;error;<br \/>\n}<\/p>\n<p>$conn-&gt;close();<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>First we achieve server connection by running the serevr connect statement.<\/p>\n<p><code>$servername = \"localhost\";<br \/>\n$username = \"username\";<br \/>\n$password = \"password\";<br \/>\n$dbname = \"myDB\";<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\n\/\/ Check connection<br \/>\nif ($conn->connect_error) {<br \/>\n    die(\"Connection failed: \" . $conn->connect_error);<br \/>\n} <\/code><\/p>\n<p>The next step is adding the value to the database by running SQL statement.<\/p>\n<p><code>$sql = \"INSERT INTO MyGuests (firstname, lastname, email)<br \/>\nVALUES ('John', 'Doe', 'john@example.com')\";<\/code><\/p>\n<p>In the above code, we have instructed the php script to <code>INSERT INTO MyGuests<\/code> column the following values <code>firstname, lastname, email<\/code>. And to each entry give the following value <code>'John', 'Doe', 'john@example.com'<\/code>.<\/p>\n<p>Remember you must add the values to the quote. Each value is separated by the comma.<\/p>\n<p>On the next line, we have checked if everything is ok by using if statement and then we closed the statement with the closing tag.<\/p>\n<p><code>if ($conn->query($sql) === TRUE) {<br \/>\n    echo \"New record created successfully\";<br \/>\n} else {<br \/>\n    echo \"Error: \" . $sql . \"<br \/>\" . $conn->error;<br \/>\n}<\/p>\n<p>$conn->close();<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once you are done with creating the table in the MySQL database. The next step in the process is adding value to the database. You<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":376,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/375\/revisions\/376"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}