CodShot
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.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support