

The next part of the code ( 20 – 22 lines) checks if our query was successful: if (mysqli_query($conn, $sql)) VALUES ('Thom', 'Vial', used curly brackets instead of the simple parenthesis. In our example, everything between the quotes and written after $sql = is an SQL query. SQL queries must be set between the quotes. For example, in our case, it would be like this: name = Test, lastname = Testing, email = thing worth mentioning is that we just ran an SQL query using PHP code. That way, each column represents a specific value. Here we specify our values to insert into the previously specified columns. If we wrote (email, lastname, name), values would be added in the wrong order. Data will be added in the specified order. Going further, between the parenthesis, we have the table column names specified to where we want to add the values: (name, lastname, email). In this example, we are adding data to the table Students. The INSERT INTO is a statement which adds data into the specified database table.

So, let’s start with line number 19: $sql = "INSERT INTO Students (name, lastname, email) VALUES ('Test', 'Testing', is the most important line of PHP code as it inserts data into the MySQL database. We will not go through this part again, however, if you wish to know what each line of code means, check out our how to connect to a database tutorial. The first part of the code (lines 3 – 18) is meant for the actual connection to the database. Here is a full PHP code example with the basic connection and insert methods: ". After that is done, we can proceed with the MySQL query INSERT. MySQLi Methodįirst, you’ll need to establish a connection to a database. The PHP MySQLi method and PHP Data Object or PDO method. There are two methods you can use to INSERT data into your MySQL database. PHP Code to INSERT Data Into MySQL Database This will automatically enumerate the entries (1,2,3,4…).Ĭlick Save and your table will be created.

We also marked “A_I”, which means Auto Increment. It is used to enumerate table entries and required when configuring table relationships. When creating a table, it is recommended to have one ID column.

Since this is a simple setup, for more information regarding the structure of a table/database and what kind of settings you can use with the columns, refer to the official documentation of phpMyAdmin.įor now, here are a few explanations of the columns that we used: After that, you will be directed to a new page where you can enter all the information needed for your table: You can create a new table by clicking the Create Table button near the bottom of the page. We will create a table named Students for our database u104357129_name.
