Monday, October 9, 2017

PHP Insert Data Into MySQL

skptricks How to Insert Data Into MySQL Database Table Using PHP

In this post, we are going to learn how to insert records in database table using PHP Script. To insert records in database table we are using PHP mysqli_query() function. This function is used to perform query operation in MySQL database.

Before we start with insert operation in MySQL database, I think we should have complete idea about how to create MySQL database table using PHP script.

NOTE : mysql_query() function is deprecated, since PHP 5.5 Version.

we are inserting below record using our script:

PHP MySQLi Insert Record Example

$host = 'localhost';  // set hostname
$user = '';   // set mysql username
$pass = '';   // set mysql password
$dbname = 'companyrecord';  
// establish database connection  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
  die('Could not connect: '.mysqli_connect_error());  
echo 'Connected to database successfully<br/>';  
// query to perform   
$sql = 'INSERT INTO employee(empname,empsalary) VALUES ("skptricks", 104000)';  
// performing the query in database  
if(mysqli_query($conn, $sql)){  
 echo "Record inserted successfully in database";  
echo "Could not insert record: ". mysqli_error($conn);  

Connected to database successfully
Record inserted successfully in database

No comments:

Post a Comment