How to insert into two different table in a database using PDO with prepare statement

Good day,
I will be glad to get help on how I can insert into two different table once in a database using PDO with prepare statement…below is my php code…

$pdo->beginTransaction();
$sql  = 'INSERT INTO members SET
first_name = :firstName,
last_name = :lastName,
user_name = :userName' ;

$s = $pdo->prepare($sql);
$s->bindValue(':firstName', $firstName);
$s->bindValue(':lastName', $lastName);
$s->bindValue(':userName', $userName);
$s->execute();


$frnd = 'INSERT INTO friends SET
friend_one = :friend_one,
friend_two = :friend_two,
status = 2';
$s = $pdo->prepare($frnd);
$s->bindValue(':friend_one', $userName);
$s->bindValue(':friend_two', $userName);
$s->execute();
$pdo->commit();

Thanks.

I wouldn’t use the same variable for different statements, but other than that it should work.

Change second variable used from $s to something else so each are unique.

I try that now, I change the second statement $s to another variable…the code do not insert…any help on rearrange code will help.

then you should enable PDO’s error reporting.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.