Coding php insert data into database
After creating a MySQL database and table, we can start inserting the data (i.e. records) in them. In this tutorial, we are going to learn how to insert data in MySQL database using PHP in XAMPP stack. Show
PrerequisitesMake sure you have setup XAMPP stack in your system. The following guide explains how to setup XAMPP stack in Linux.
Alternatively, you can use the LAMP or LEMP stacks which provide both PHP and MySQL. If you're on Linux, refer to the following guides to install LAMP/LEMP stacks.
Setting up XAMPP is much easier than LAMP and LEMP stacks. So, we will will be using XAMPP stack throughout this guide. After setting up the XAMPP stack, you need to create a MySQL database and table inside the database. Refer to the following guide to know how to create MySQL database and table in XAMPP stack.
For demonstration purpose, I am going to create a table named "sales" in a database called "my_company" in my XAMPP stack. Insert Data In MySQL Database Using PHPWe can insert a single record or multiple records at once in a MySQL table. First, we will see how to insert single record. Inserting Single Record In MySQL Table Using PHPWe will use a SQL Query statement in our PHP
code to insert a record in the table. We can insert a single record/row into a table by using the Query Syntax: INSERT INTO table_name(column1,column2,………….) VALUES (value1,value2,………….) (Or) INSERT INTO table_name VALUES (value1,value2,………….) Where, table_name represents the name of the table and columns represents the column name and values are inserted into the table. Steps1. Specify the servername, MySQL username, password and database name in the PHP code. Here, the servername is
localhost, username is 2. Create a connection using the above details. By using Code: $connection = mysqli_connect($server_name, $user_name, $password,$database_name) 3. Check the Connection. We can check the connection using the 4. Specify the SQL Query to insert a single record into the table. In this step, we specify the SQL query to insert values into the database. Let the database name is "my_company" and we are storing it in a variable named query. The table name is "sales" that has three columns. Code: $query = "INSERT INTO table_name (id,name,count) VALUES (1,'Cooking-Gas',40)"; 5. Checking the creation. If we want to check the values inserted or not, we can use the Code: mysqli_query($connection, $query) It will take two parameters.
If this condition is True, then a row will be inserted. Otherwise it will return an error. We can check the error by using the 6. Close the connection. This is the last step where we have to close the connection by using the Code: mysqli_close($connection); Now let us write a sample PHP code based on the above steps. PHP Code To Insert A Single Record In MySQL TableCreate a new file named Heads Up: If you use Linux, the htdocs folder will be under In the above code, "my_company" is the database name and "sales" in the table name. Open your web browser and navigate to http://localhost/insert.php. You will see an output like this in your browser window. Insert A Single Record In MySQl TableYou can also verify if the record/row is inserted from phpMyAdmin as well. To do so, open phpMyAdmin dashboard by navigating to http://localhost/phpmyadmin URL from the browser. Go to my_company -> sales and click Browse to see the inserted record. View Records In MySQL TableAs you can see, we have successfully inserted a single record in the table called "sales" in the my_company database. Inserting Multiple Records In MySQL Table Using PHPWe can insert multiple Records/Rows into a table by using the Query Syntax: INSERT INTO table_name(column1,column2,………….) VALUES (value1,value2,………….) (Or) INSERT INTO table_name VALUES (value1,value2,………….) Where, table_name represents the name of the MySQL table and columns represents the column name and values are inserted into the table. Steps1. Specify servername, MySQL username, password and database name. Here, the servername is localhost, username is root and password is empty. And the database name is "my_company", and we are creating a table inside this database. 2. Create a connection using the above details. By using Code: $connection = mysqli_connect($server_name, $user_name, $password,$database_name); 3. Check the Connection. We can check the connection using 4. Specify the SQL Query to insert multiple records into the table. In this step, we specify the SQL query to insert values into the database in a variable. Let the database name be my_company and we are storing in a variable named query. The table name is sales that has three columns. Code: $query = "INSERT INTO table_name (id,name,count) VALUES (5,'Shampoo',10);"; $query .= "INSERT INTO table_name (id,name,count) VALUES (2,'Milk',20);"; $query .= "INSERT INTO table_name (id,name,count) VALUES (3,'Books',14);"; $query .= "INSERT INTO table_name (id,name,count) VALUES (4,'Chocos',45);"; $query .= "INSERT INTO table_name (id,name,count) VALUES (6,'Eggs',12)"; Heads Up: Please mind the dot (.) in second and the subsequent SQL queries. You need to put a dot (.) in front of the equal sign starting from second query. 5. Checking the creation. If we want to check if the values inserted or not, we can use Code: mysqli_query($connection, $query) As you can see, It takes two parameters.
If this
condition is True, then the rows will be inserted. Otherwise it will return an error. We can check the error by using the 6. Close the connection. This is the last step where we have to close the connection by using the Code: mysqli_close($connection); Now let us write a sample PHP code to insert 5 records based on the above steps. PHP Code To Insert Multiple Records In MySQL TableCreate a new file named Open your web browser and navigate to http://localhost/insert_multiple.php. You will see an output like this in your browser window. Insert Multiple Records In MySQL TableLet us verify if the records are actually inserted or not from the phpMyAdmin dashboard. Open phpMyAdmin dashboard by navigating to http://localhost/phpmyadmin URL from the browser. Go to my_company-> sales and click on Browse button to see the inserted records. View Records In MySQL Table Via PhpMyAdminConclusionIn this tutorial, we discussed how to insert data in MySQL database using PHP code in XAMPP stack. We provided two sample PHP codes to demonstrate how to insert single and multiple records in a MySQL table. Change the code as per your requirement and test them on your system. If you have any question, let us know via the comment section below. Read Next:
How we can insert data in database using PHP?The INSERT INTO statement is used to add new records to a MySQL table: INSERT INTO table_name (column1, column2, column3,...). The SQL query must be quoted in PHP.. String values inside the SQL query must be quoted.. Numeric values must not be quoted.. The word NULL must not be quoted.. How do you add data to a database?Simple INSERT statement to add data to the table. Use INSERT Statement to add multiple rows in the table. INSERT INTO SELECT clause to insert the output generated by the SELECT query. INSERT IGNORE clause to ignore the error generated during the execution of the query.
How can insert data in database in HTML?For this you need to follow the following steps:. Step 1: Filter your HTML form requirements for your contact us web page. ... . Step 2: Create a database and a table in MySQL. ... . Step 3: Create HTML form for connecting to database. ... . Step 4: Create a PHP page to save data from HTML form to your MySQL database. ... . Step 5: All done!. What is insert into in PHP?In PHP, we use the INSERT INTO statement to add new rows to a database table. By passing mysqli query to PHP, we can run the insert query(). We use mysqli multi query to execute multiple queries in a single call because mysqli query cannot execute multiple queries to avoid SQL injections.
|