Program to Insert data using PHP in mysql.

Here, we will write a program in php to insert data in Mysql. Considering we have created the connection, database and the table.

insert.php file

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Insert data
$sql = "INSERT INTO mytable (firstname, lastname, email)
VALUES ('rahul', 'kumar', 'abc@gmail.com')";

if ($conn->query($sql) === TRUE) {
  echo "Data inserted successfully";
} else {
  echo "Error inserting data: " . $conn->error;
}

$conn->close();
?>