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 :

_______________________________________________________________________________

Hi,

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 −

 

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.

 

<?php
/* Insert data into table */
$host = “localhost”;
$user = “root”;
$pass = “”;
$connect=mysql_connect($host, $user, $pass);
mysql_select_db(“trainee”);
$query=”Insert into details values(‘Guhan’, ‘Ganesan’, ‘979181****’)”;
$mysql=mysql_query($query);
if($mysql)
{
echo “The Data is inserted into the table is success!!”;
}
?>

OUTPUT:

The Data is inserted into the table is success!!

/*

Name Father Mobile
Guhan Ganesan 979181****

*/

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.

REFERENCES :

https://www.tutorialspoint.com/php/mysql_insert_php.htm