Inserting Data in Table through PHP – With Coding
This blog explains about Inserting Data in Table through PHP – With Coding . It is illustrated very well below :
We have learned how to create table in last program. This program shows how to insert new data into the created table. Ok, Lets see the program
We have divided this chapter in the following sections −
- Connecting to MySQL database − Learn how to use PHP to open and close a MySQL database connection.
- Create MySQL Database Using PHP − This part explains how to create MySQL database and tables using PHP.
- Delete MySQL Database Using PHP − This part explains how to delete MySQL database and tables using PHP.
- Insert Data To MySQL Database − Once you have created your database and tables then you would like to insert your data into created tables. This session will take you through real example on data insert.
- Retrieve Data From MySQL Database − Learn how to fetch records from MySQL database using PHP.
- Using Paging through PHP − This one explains how to show your query result into multiple pages and how to create the navigation link.
- Updating Data Into MySQL Database − This part explains how to update existing records into MySQL database using PHP.
- Deleting Data From MySQL Database − This part explains how to delete or purge existing records from MySQL database using PHP.
- Using PHP To Backup MySQL Database − Learn different ways to take backup of your MySQL database for safety purpose.
Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query. Below a simple example to insert a record into employee table.
/* Insert data into table */
$host = “localhost”;
$user = “root”;
$pass = “”;
$connect=mysql_connect($host, $user, $pass);
$query=”Insert into details values(‘Guhan’, ‘Ganesan’, ‘979181****’)”;
echo “The Data is inserted into the table is success!!”;
The Data is inserted into the table is success!!
In real application, all the values will be taken using HTML form and then those values will be captured using PHP script and finally they will be inserted into MySQL tables.
While doing data insert its best practice to use function get_magic_quotes_gpc() to check if current configuration for magic quote is set or not. If this function returns false then use function addslashes() to add slashes before quotes.