PHP | Inserting into MySQL database
Inserting form data securely into MySQL database using PHP PDO prepared statements.
<?php
$pdo = new PDO("mysql:host=localhost;dbname=company_db", "root", "password");
$stmt = $pdo->prepare("INSERT INTO employees (first_name, last_name, email, salary, hire_date) VALUES (:fn, :ln, :email, :sal, :hd)");
$stmt->execute([
':fn' => 'Sarah',
':ln' => 'Connor',
':email' => 'sarah.c@example.com',
':sal' => 65000.00,
':hd' => '2026-09-01'
]);
echo "Record inserted via PHP PDO! Inserted ID: " . $pdo->lastInsertId();
?>Record inserted via PHP PDO! Inserted ID: 6