Posted By:
7 Mar, 2014 11:11 pm PST

How to insert user data into MySql database from PHP application?

 How to insert user data into MySql database from PHP application?

Once you make connection with database, you are ready to perform SQL queries on that database. Now you can insert , update or view data using PHP method.

To insert data into table insert query is used

Syntax:- 

Insert into table_name (column1, column2, column3, column4,… )

Into (value1, value2, value3, value4….) ;

Or

Insert into table_name

Into (value1, value2, value3, value4….);

 

This query inserts data into specified table;

mysql_query () method is used to execute all query (select, update, insert, alter, drop) of sql.

 

Syntax:- 

mysql_query(query);

 

query:- query that you want to execute;

 

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

 

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

 

//Create PHP application that will accept the employee data and will store in database 

 
 

<? php

function getConnection ()

{

$Connect = mysql_connect ( " localhost " , " root " , " " ) ;

// connects to the localhost

$db = mysql_select_db ( " Company " , $Connect ) ;

// connects to the specified database ;

return $db ;

}

?>

Connection.php

 

 

 

 

 

 

 

 

 

 

 

<?php

require_once ’ Connection . php ’;

$eid = $_POST [ ’ eid ’ ];

$fname = $_POST [ ’ fname ’ ];

$lname = $_POST [ ’ lname ’ ];

$dob = $_POST [ ’ dob ’ ];

$dept = $_POST [ ’ dept ’ ];

$sal = $_POST [ ’ sal ’ ];

echo " { $eid } < br >{ $fname } < br >{ $lname } < br >{ $dob } < br >{ $dept } < br >{$sal } " ;

echo " <h3 > " ;

$db = getConnection () ; // making connection to database

 

if ( $db )

{

echo " Database connceted Successfully < br > < br > " ;

}

else

{

echo " Database not connceted Successfully < br > < br > " ;

}

$query = " INSERT INTO employee VALUES ({ $eid } , ’{ $fname } ’ ,

’{ $lname } ’ , ’{ $dob } ’ , ’{ $dept } ’ ,{ $sal }) " ; // insert query

echo " <br > { $query } <br > " ;

$res = mysql_query ( $query ) ; // executing query

 

if ( $res )

{

echo " record inserted successfully " ;

}

else

{

echo " record not inserted " ;

} 

?>

InsertQ uery.php

 

DataInsertPage

  

InsertQueryResult 

 

 

 

 

Attachment: InsertQuery.php


//Code to insert data into MySql through PHP
 
<html 
<body>
<?php
$con=mysqli_connect("localhost","root","root","mydbse1");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL Database: " .mysqli_connect_error();
}
$sql="INSERT INTO mydb values('priya','felice',28)";
if (mysqli_query($con,$sql))
{
echo "Values inserted successfully in mydb table.";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
</body>
</html>
 
Attachment: Insert.php
Was it useful?  




Please login to reply to this problem.