How to Insert data in MySQL table through PHP

In this article I am going to explain about insert records in MySQL.
  • 2043

Insert data into MySQL table in PHP

The INSERT INTO statement is used to inserting data into MySQL tables.


Syntax

You can follow two types of syntax for inserting records in database tables:

  • First syntax

    In this syntax, columns name does not specified.

INSERT INTO tableName values(value1, value2, value3,.......)

  • Second syntax

    In this syntax, both colums name and values are specified.

INSERT INTO tableName (column1, column2, column3,......) values(value1, value2, value3,,.......)

 

Example of inserting data into MySQL table
 

<html>

<body>

<?php

$con = mysql_connect("localhost","gupta","sharad");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

else

{

echo "value is inserted.";

}

 

mysql_select_db("Mcn_Solution", $con);

 

mysql_query("INSERT INTO Emp (Fname , LastName, Id)  // .....Second syntax

VALUES ('Dinesh', 'singh',101)");

 

mysql_query("INSERT INTO Emp VALUES ('Manish', 'kumar',102)");   // ..... First syntax

 

mysql_close($con);

?>

</body>

</html>


Output:

insert data in table.jpg

You may also want to read these related articles :
here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.