Thuta Learning
AdvancedProgrammingbeginner

INSERT Data

Relax. We'll talk through this in plain words — no textbook voice.

An INSERT query is used to store new data. You need INSERT for things like contact form submissions, new user registration, or creating a product. When saving form data, validate it first, then save it with a prepared statement.

php
<?php
$name = "Aung Aung";
$email = "aung@example.com";
$message = "I want to learn PHP.";

$stmt = $conn->prepare("INSERT INTO contacts (name, email, message) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $message);

if ($stmt->execute()) {
  echo "Contact message saved.";
} else {
  echo "Something went wrong.";
}

$stmt->close();
?>
You should see
Contact message saved.

Easy traps

  • Running INSERT without validation can let blank rows sneak into the database.
INSERT Data | Thuta Learning