Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

MySQL Insert Data

PHP

MySQL Insert Data

INSERT statement MySQL table में नया data जोड़ने के लिए उपयोग किया जाता है। PHP में हम MySQLi Procedural और PDO दोनों तरीकों से data insert कर सकते हैं। INSERT query किसी भी CRUD (Create, Read, Update, Delete) application का पहला और सबसे आवश्यक हिस्सा है — जैसे कि user registration, form submission, order creation, blog posting आदि।

  • INSERT को table में नए records जोड़ने के लिए उपयोग किया जाता है।
  • AUTO_INCREMENT column (जैसे id) अपने आप नया unique ID generate करता है।
  • Form data insert करते समय SQL Injection को रोकने के लिए prepared statements ज़रूरी हैं।

Basic SQL Syntax

Basic INSERT Syntax
INSERT INTO students (name, class, city) VALUES ('Riya', 9, 'Mumbai');

Important Notes

  • INSERT query में सभी column names और उनके correct data types match होने चाहिए।
  • Quotes (') गलत होने पर query break हो सकती है — prepared statements का उपयोग करें।
  • Form inputs को हमेशा verify/validate करें।
  • NULL और empty values के साथ सावधान रहें — यदि column NOT NULL है तो error आएगा।

MySQLi Procedural – Insert Data

यह example दिखाता है कि simple INSERT query कैसे execute की जाती है:

उदाहरण: MySQLi Procedural (Simple INSERT)
 <?php
   $conn = mysqli_connect("localhost", "root", "", "school");
   $sql = "INSERT INTO students (name, class, city) VALUES ('Riya', 9, 'Mumbai')";
   if (mysqli_query($conn, $sql)) {
      echo "Record Inserted Successfully";
   } else {
      echo "Insert Error: " . mysqli_error($conn);
   }
   mysqli_close($conn);
 ?> 

MySQLi Procedural – Prepared Statement (Recommended)

Prepared statements injection attacks से बचाते हैं और सबसे सुरक्षित तरीका हैं:

MySQLi Prepared INSERT
 <?php
   $conn = mysqli_connect("localhost", "root", "", "school");
   $stmt = mysqli_prepare($conn, "INSERT INTO students (name, class, city) VALUES (?, ?, ?)");
   mysqli_stmt_bind_param($stmt, "sis", $name, $class, $city);
   $name = "Kunal";
   $class = 11;
   $city = "Kolkata";
   mysqli_stmt_execute($stmt);
   echo "Prepared Statement Inserted!";
   mysqli_stmt_close($stmt);
   mysqli_close($conn);
 ?> 

PDO – Insert Data

PDO database-independent layer है और prepared statements के साथ optimal काम करता है:

उदाहरण: PDO Simple INSERT
 <?php
   $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
   $sql = "INSERT INTO students (name, class, city) VALUES ('Meena', 7, 'Surat')";
   $pdo->exec($sql);
   echo "Record Inserted with PDO";
 ?> 

PDO – Prepared Statement (Highly Recommended)

PDO Prepared INSERT
 <?php
   $stmt = $pdo->prepare("INSERT INTO students (name, class, city) VALUES (?, ?, ?)");
   $stmt->execute(["Meena", 7, "Surat"]);
   echo "Prepared Insert Successful";
 ?> 

PDO Named Placeholder Example

PDO Named INSERT
 <?php
  $stmt = $pdo->prepare(" INSERT INTO students (name, class, city) VALUES (:name, :class, :city) ");
  $stmt->execute([":name" => "Tina", ":class" => 8, ":city" => "Jaipur"]);
  echo "Prepared Insert Successful";
 ?> 

Common Mistakes (और समाधान)

  • गलती: Quotes गलत लगना → SQL error
    Fix: Prepared statements हमेशा उपयोग करें।
  • गलती: NOT NULL column में blank insert करना
    Fix: Default values या validations ज़रूरी।
  • गलती: Wrong data type (string → int)
    Fix: सही binding types (i, s, d) का उपयोग करें।
  • गलती: Special characters ( ' " & ) से query break
    Fix: $stmt->execute() values को auto-escape करता है।

Additional Example – Insert With Last ID

Record insert करने के बाद आप last inserted ID भी प्राप्त कर सकते हैं:

INSERT + Last ID (MySQLi)
mysqli_query($conn, $sql);
echo "New ID: " . mysqli_insert_id($conn);

Best Practices

  • Always use prepared statements (MySQLi/PDO)
  • Validate user inputs before inserting
  • Use transactions for multiple inserts
  • Insert only required columns (avoid unnecessary fields)
  • Error handling implement करें (try/catch)

Conclusion

INSERT statement database में new records जोड़ने का सबसे मूल और महत्वपूर्ण हिस्सा है। PHP में इसे MySQLi और PDO दोनों के साथ आसानी से execute किया जा सकता है। Prepared statements injection से बचाव और reliability के लिए सबसे सुरक्षित तरीका हैं। यदि आप secure और scalable applications बनाना चाहते हैं, तो simple string queries की बजाय prepared statements और validations का उपयोग करें।

Article By: Brajlal Prasad
Created on: 04 Nov 2025  8  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store