Insert record into MySQL Database Using PHP Language
Object-oriented
<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here
// Create connection इस लाइन में यहाँ ये बताया है की कैसे कनेक्शन किया है
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection इस लाइन पर चेक किया है की आपका डेटाबेस से कनेक्शन हुआ है की नहीं
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Insert Record Query इस लाइन पर डाटा टेबल में डालने की query लिखी है
$sql = "INSERT INTO tableName (name, class, subject)
VALUES ('Sanju Yadav', '12th', 'Math)";
//check record insert or not
if ($conn->query($sql) === TRUE) {
echo "Your record has been Inserted successfully";
} else {
echo "Your record has Not been Inserted successfully";
}
$conn->close();
?>
Procedural
<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here
// Create connection इस लाइन में यहाँ ये बताया है की कैसे कनेक्शन किया है
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection इस लाइन पर चेक किया है की आपका डेटाबेस से कनेक्शन हुआ है की नहीं
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//Insert Record Query इस लाइन पर डाटा टेबल में डालने की query लिखी है
$sql = mysqli_query("INSERT INTO tableName (name, class, subject)
VALUES ('Sanju Yadav', '12th', 'Math)") or die(mysqli_query());
//check record insert or not
if(mysqli_affected_rows($conn) >0) {
echo "Your record has been Inserted successfully";
} else {
echo "Your record has Not been Inserted successfully";
}
mysqli_close($conn);
?>
Quick ask your Question
- Connect mysql database using php Language
- Insert record into MySQL Database Using PHP Language
- Get Record from MySQL Database in PHP
- Update Record from MySQL Database in PHP
- Delete Record From a MySQL Database using PHP
- Insert Multiple Records into MySQL with a Single Query
- Create MySQL Database Using PHP
- Create Table in MySQL Database Using PHP