PHP PDO: Insert working without attributes

Stumbler

This question is about why something is working.

Using unnamed placeholders in PHP PDO:

$STH  = $connection->prepare("INSERT INTO Person (firstname, lastname, age) values ("?, ?, ?")");
$STH->execute($the_data);

This sort of insertion works correctly.

However, it still works if written as

$STH  = $connection->prepare("INSERT INTO Person () values ("?, ?, ?")");
$STH->execute($the_data);

Huh? Are attribute names just syntactic sugar or something?

Artur Zhdanov

You need to bind values before execute. This is right way:

$STH  = $connection->prepare('INSERT INTO Person (firstname, lastname, age) values (?, ?, ?)');
$STH->bindValue(1, 'MyFirstName', PDO::PARAM_STR);
$STH->bindValue(2, 'MyLastName', PDO::PARAM_STR);
$STH->bindValue(3, 28, PDO::PARAM_INT);
$STH->execute();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related