How to insert value in MySQL database

How to insert value in MySQL database

In this article we are going to tell you:


  • What is MySQL?
  • Inserting value in database means.
  • writing the INSERT query.
  • Ordinary and preferred ways of using this command.
  • Source Code.




What is MySQL?

              It is an open source relational database system also named as RDBMS. It is based on structured query language (SQL). And Php is used as a scripting  Language. The application is used for a vast range of purposes. It includes data warehousing, e-commerece, and logging application. It is most commonly used as a web database.

What do you mean by inserting a value in database?

          Inserting a value in database means to put user's data in the database against a specific id.
For this purpose their is a command in SQL. INSERT INTO is a command for this purpose.

How to write INSERT query?

    Now i will tell you how to use the insert query to store the value in database. And its very simple :).

Query:


  1. By Passing values directly in query:

  • First of all declare a variable in which query should be stored (i.e  $insert).
  • Now start the double inverted commas.
  • Write INSERT INTO in the commas.
  • Than the name of table in the database.
  • Start parenthesis right after the table name.
  • Write the names of columns which you have given in the database.
  • These names should be separated from each other by commas.
  • After parenthesis write the command VALUES.
  • Start parenthesis to put the or values.
  • write the values in single inverted commas.
  • And these values should be separated by comma.


#Source Code:

<?php


$server="localhost";    
      $username="username";
      $pass="";
      $database="database_name"; 
      $con=mysqli_connect($server,$username,$pass,$database);
       if (!$con)
          {   
              die( "Connection Failed : " .mysqli_error($con) );    
           }
else{
$insert =   "INSERT INTO table_name (column1,column2,column3) 
  VALUES
('value1','value2','value3')";

}
?>

  • By Passing Variables  (preferred)


  • The other way to use insert query is by declaring variables before the query.
  • Assign the data of form submitted by user to these variable.
  • This should be through GET or POST method.
  • Then following all the above steps EXCEPT the steps to write VALUES.
  • Inside the VALUES CLAUSE the declared variables should be used.
  • In single inverted comma we should write the variables (i.e $value1).





Why PREFERRED way to be chosen?

             The second method is used most probably because user will input the values in the form shown to him. Not the code is shown to him in which he can put values inside the INSERT query. So declaring a variable for a specific input and using it in the INSERT query is the BEST option.

#Source code

<?php

$server="localhost";    
      $username="username";
      $pass="";
      $database="database_name"; 
      $con=mysqli_connect($server,$username,$pass,$database);
       if (!$con)
          {   
              die( "Connection Failed : " .mysqli_error($con) );    
           }
else{

$insert =   "INSERT INTO table_name (colomn1,colomn2,colomn3) 
 VALUES
('$value1','$value2','$value3')";
}

?>

Note:

Hope this article was useful to you.
And be with us for more articles relating Php.
You can also comment below the problems you are facing so that we can help you. :)

And to know how to insert these values in the Database Click the Link.

To create connection to database. Click the Link.


Related Post: