Introduction

Hi guys, In this article we are going to have a glance at the insert into statement through php. The INSERT INTO statement is used to insert new records in a table. Here we are going to explore the concept thoroughly. You must have the required XAMPP server; if you do not have XAMPP server then go to the link

http://www.c-sharpcorner.com/UploadFile/c8aa13/installation-of-xampp-server-to-run-php-program/

Note : To create the database go to http://www.c-sharpcorner.com/UploadFile/c8aa13/creation-of-database-table-through-php/

Insert Data Into a Database Table

Here in this section, the INSERT INTO statement is used to add new records to a database table.

Syntax

We can use the insert into command in two forms. It is possible to write the INSERT INTO statement in two forms.

  • In this it doesn't specify the column names where the data will be inserted, only their values.
    1. INSERT INTO table_name VALUES (value1, value2, value3,..)
  • The second form specifies both the column names and the values to be inserted.
    1. INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

PHP Script for inserting the data into table

  1. <html>
  2. <body bgcolor="skyblue">
  3. <center>
  4. <h3> Inserting data into database table ! <h3>
  5. <hr>
  6. <?php
  7. $con = mysql_connect("localhost","root","");
  8. if (!$con)
  9. {
  10. die('Could not connect: ' . mysql_error());
  11. }
  12. mysql_select_db("dwij", $con);
  13. mysql_query("INSERT INTO PBL (FirstName, LastName, Age) VALUES ('DEEPAK', 'DWIJ', '25')");
  14. mysql_query("INSERT INTO PBL (FirstName, LastName, Age) VALUES ('Prerna', 'Kaul', '23')");
  15. echo "Record inserted into table PBL ! ";
  16. mysql_close($con);
  17. ?>
  18. </center>
  19. </body>
  20. </html>
Save it by insert.php

Output

To run the code, Open the XAMPP server and start the services like Apache and MySQL. Open the browser and type: http://localhost/yourfoldername/insert.php

inserted1.gif

To show where the data is inserted, open the browser and type in the url:

http://localhost/phpmyadmin/index.php?db=dwij&token=6af47d988e2f11114a03bd72341e49c4

ins2.gif

Insert Data From a Form Into a Database

Here in this section we will insert data into a database table from the front end.

HTML Script

  1. <html>
  2. <body bgcolor="Lightgreen">
  3. <center>
  4. <h2> Inserting Data into PBL table ! <h2>
  5. <hr>
  6. <form action="phppage.php" method="post">
  7. Firstname: <input type="text" name="firstname"><br><br>
  8. Lastname: <input type="text" name="lastname"><br><br>
  9. Age: <input type="text" name="age"><br><br><br>
  10. <input type="submit">
  11. </form>
  12. </center>
  13. </body>
  14. </html>

Save it as Main.php.

PHP Script

  1. <html>
  2. <body bgcolor="Skyblue">
  3. <center>
  4. <h3> Data Inserted into PBL table ! <h3>
  5. <hr>
  6. <?php
  7. $con = mysql_connect("localhost","root","");
  8. if(!$con)
  9. {
  10. die('Could not connect: ' . mysql_error());
  11. }
  12. mysql_select_db("dwij", $con);
  13. $sql="INSERT INTO PBL (FirstName, LastName, Age)
  14. VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
  15. if (!mysql_query($sql,$con))
  16. {
  17. die('Error: ' . mysql_error());
  18. }
  19. echo "1 record added";
  20. mysql_close($con)
  21. ?>
  22. </center>
  23. </body>
  24. </html>
Save it as phppage.php.

Output

To run the code, Open the XAMPP server and start the services like Apache and MySQL. Open the browser type:

http://localhost/yourfoldername/Main.php

ins3.gif

After clicking on the submit Query button the next page will be as follows:

ins4.gif

To show where the data is inserted, open the browser and type in the url http://localhost/phpmyadmin/

ins5.gif

Select the database 'dwij' from the left corner of the figure shown above.

ins6.gif

Now click on the 'pbl' from the left corner of the page shown above; you will get the final result.

ins7.gif

Conclusion: Inserting data from the front end is an important task; now you can easily work with Insert statement with PHP script.

Thanks!