{"id":3501,"date":"2019-11-19T16:10:53","date_gmt":"2019-11-19T21:10:53","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=3501"},"modified":"2019-11-19T16:15:29","modified_gmt":"2019-11-19T21:15:29","slug":"inserting-data-into-a-mysql-database-using-php-and-mysqli","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli","title":{"rendered":"Inserting Data Into a MySQL Database Using PHP and MySQLi"},"content":{"rendered":"\n

This tutorial will show you how to insert data into a MySQL database using the latest MySQLi PHP libraries. This tutorial will replace our older tutorial on MySQL libraries and that post will redirect here in the future.<\/p>\n\n\n\n

Assumptions<\/h2>\n\n\n\n

First, we will assume you have PHP, MySQL, and Apache running. I use MAMP<\/a> for my local development environment in this case. You can download it for free on both Windows and Mac. We also need a SQL client to connect to the database, I’m using Sequel Pro<\/a> on Mac. You may want to use Heidi SQL<\/a> on Windows.<\/p>\n\n\n\n

To set up this tutorial I’ve created a database with a table called users<\/strong>. The schema is as follows:<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

<\/p>\n\n\n\n

Inserting Data into the MySQL Database Using PHP<\/h2>\n\n\n\n

First, we’re going to set up our connection to the MySQL database. We’re going to set our server name, user name, password, and database name. Then we’re going to make a connection to the server. If there is a connection error we will output the error message. Otherwise, we are not going to do anything else for now.<\/p>\n\n\n

\n<?php\n$servername = "localhost";\n$username = "root";\n$password = "root";\n$dbname = "test_db";\n\n$conn = new mysqli($servername, $username, $password, $dbname);\n\nif ($conn->connect_error) {\n   die("MySQL Connection failed: " . $conn->connect_error);\n}\n?>\n<\/pre><\/div>\n\n\n

Now we will create and execute our insert query. As you see below we create a query variable and write our insert query. Then we execute the query. If there is an error we display it otherwise we display that the database has been updated.<\/p>\n\n\n

\n<?php\n$servername = "localhost";\n$username = "root";\n$password = "root";\n$dbname = "test_db";\n\n$conn = new mysqli($servername, $username, $password, $dbname);\n\nif ($conn->connect_error) {\n   die("MySQL Connection failed: " . $conn->connect_error);\n}\n\n$query = "INSERT INTO users (name, username) VALUES ('John','spyderman4g63')";\n\nif (!$conn->query($query)){\n    echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;\n}else{\n    echo "The database has been updated";\n}\n\n$conn->close();\n?>\n\n<\/pre><\/div>\n\n\n

Viewing the Data in the Database<\/h2>\n\n\n\n

I’m going to look at the table in my SQL tool, which is Sequel Pro<\/a> on my Mac. Below we can see that the application has inserted one row for me.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

That’s all there is to insert data into a MySQL database using the MySQLi libraries in PHP. Check out this tutorial if you want to display MySQL data on a page using MySQLi in PHP<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

This tutorial will show you how to insert data into a MySQL database using the latest MySQLi PHP libraries. This tutorial will replace our older tutorial on MySQL libraries and that post will redirect here in the future. Assumptions First, we will assume you have PHP, MySQL, and Apache running. I use MAMP for my … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":3507,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"yoast_head":"\nInserting Data Into a MySQL Database Using PHP and MySQLi<\/title>\n<meta name=\"description\" content=\"This tutorial will show you how to insert data into a MySQL database using the latest MySQLi PHP libraries. This tutorial will replace our older tutorial\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Johnathan Ward\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/php-mysqli.png\",\"contentUrl\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/php-mysqli.png\",\"width\":720,\"height\":200},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli\",\"name\":\"Inserting Data Into a MySQL Database Using PHP and MySQLi\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#primaryimage\"},\"datePublished\":\"2019-11-19T21:10:53+00:00\",\"dateModified\":\"2019-11-19T21:15:29+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\"},\"description\":\"This tutorial will show you how to insert data into a MySQL database using the latest MySQLi PHP libraries. This tutorial will replace our older tutorial\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inserting Data Into a MySQL Database Using PHP and MySQLi\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\",\"name\":\"Johnathan Ward\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"caption\":\"Johnathan Ward\"},\"description\":\"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.\",\"sameAs\":[\"http:\/\/johnathanward.com\",\"https:\/\/twitter.com\/spyderman4g63\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inserting Data Into a MySQL Database Using PHP and MySQLi","description":"This tutorial will show you how to insert data into a MySQL database using the latest MySQLi PHP libraries. This tutorial will replace our older tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli","twitter_misc":{"Written by":"Johnathan Ward","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#primaryimage","inLanguage":"en-US","url":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/php-mysqli.png","contentUrl":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/php-mysqli.png","width":720,"height":200},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli","name":"Inserting Data Into a MySQL Database Using PHP and MySQLi","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#primaryimage"},"datePublished":"2019-11-19T21:10:53+00:00","dateModified":"2019-11-19T21:15:29+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9"},"description":"This tutorial will show you how to insert data into a MySQL database using the latest MySQLi PHP libraries. This tutorial will replace our older tutorial","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/inserting-data-into-a-mysql-database-using-php-and-mysqli#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Inserting Data Into a MySQL Database Using PHP and MySQLi"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9","name":"Johnathan Ward","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","caption":"Johnathan Ward"},"description":"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.","sameAs":["http:\/\/johnathanward.com","https:\/\/twitter.com\/spyderman4g63"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/3501"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=3501"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/3501\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media\/3507"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=3501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=3501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=3501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}