How to insert data in sql?

insert data into database table in php


In this tutorial you'll learn how to Insert Data in MySQL table using PHP.

SQL Create Table

SQL tutorial



insert data into MySql Table tutorial


  /_?php
  
   $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
    

//This creates a connection to the MySQL server on Localhost with the username “client” and the password “client123”.


   mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));
      

//Connect to the database, if not then show error

//Now that you have created the TABLE, you are ready to insert data into the table by using the INSERT statement.
//To insert data into a table
//Execute the following statement to insert a row into the Table

  $sql = “INSERT INTO dbo.Products (ProductID, ProductName, Price, ProductDescription)
       VALUES (1, ‘Tires', 5000, ‘Tires for Toyota')”; 

   if($result = mysqli_query($sql, $cn)){       // true then
       echo "Records added successfully.";
   }
    
    else {                  // false then
        echo "ERROR: Could not able to execute $sql. “;
     
      mysql_close($cn);
  ?-/
	

All together

    /_?php
     $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
        
     mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));
      
    $sql = “INSERT INTO dbo.Products (ProductID, ProductName, Price, ProductDescription)
       VALUES (1, ‘Tires', 5000, ‘Tires for Toyota')”; 

   if($result = mysqli_query($sql, $cn)){       // true then
       echo "Records added successfully.";
   }
    
    else {                  // false then
        echo "ERROR: Could not able to execute $sql. “;
     
      mysql_close($cn);

    mysql_close($conn);
      
    ?-/
    

After execution you can see the result

Records added successfully.

insert-data-into-mysql-table

Congratulation its working 100%