PHP
Public
PDO Insert with Named Parameters
Insert a row safely with PDO named parameters.
#php
#pdo
#security
#sql
PHP
<?php
$sql = "INSERT INTO contacts (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':name' => trim($_POST['name'] ?? ''),
':email' => trim($_POST['email'] ?? ''),
]);
$contactId = (int) $pdo->lastInsertId();
Notes
Prepared statements keep user input separate from the SQL query.