0

Like this my pdo statement is working well:

$pdo = $db->prepare("UPDATE animals SET name =? WHERE id=LAST_INSERT_ID();");
$pdo->execute(array($name)); 

If I am using more prepare statements, the UPDATE statement is not working anymore:

$pdo = $db->prepare('INSERT INTO animals (age) values(:age)');
$pdo->execute(array(':age' => $_POST['age'],));

$pdo = $db->prepare('INSERT INTO people (name) values(:name)');
$pdo->execute(array(':name' => $_POST['name'],));

$pdo = $db->prepare("UPDATE animals SET age =? WHERE id=LAST_INSERT_ID();");
$pdo->execute(array($age));

But now my "update" statement is not working anymore.

peace_love
  • 6,229
  • 11
  • 69
  • 157
  • 1
    How about give different variable names? `$stmt1` `$stmt2`. And `close()` the statments after using it. – JustOnUnderMillions Dec 22 '16 at 12:42
  • I tried to use the variables $pdo1 and $pdo2, but I get an sql error message `Undefined variable` – peace_love Dec 22 '16 at 12:45
  • No, sorry I did a mistake, it is working with $pdo1 and $pdo2 – peace_love Dec 22 '16 at 12:47
  • Tried where? Please give full information here, thanks. – JustOnUnderMillions Dec 22 '16 at 12:47
  • @JustOnUnderMillions Your suggestion just worked fine! Thank you! – peace_love Dec 22 '16 at 12:49
  • @Jara Last note from me, because you are still stuck: If you are insert into one table something, at this point you have a new `LAST_INSERT_ID()` from that table, but why should this ID also exists in another table at this point? Thing about that! `id=LAST_INSERT_ID();` this is the issue you have. Why should `animals` have IDs that are created just seconds before?!?!? And by the way: Why have animals and people the same name???? – JustOnUnderMillions Dec 22 '16 at 14:59
  • @JustOnUnderMillions Thank you very much! (I do not think your answer is fraud) I updated my question to be more clear. I have actually three prepare statement. I just didn't posted the first one, because it didn't have something to do with the problem – peace_love Dec 22 '16 at 15:05
  • I finally found the answer. Now everything works fine. I would post it but am not allowed to answer my own question here. I do not have to write pdo1, pdo2. I can leave pdo. I only have to change this line `$pdo = $db->prepare("UPDATE animals SET age =? ORDER BY id DESC LIMIT 1;");` So simple! :) – peace_love Dec 22 '16 at 15:35
  • 1
    this will lead to a data loss. better ask a question how to update your data, explaining what table you want to update with which value and why. – Your Common Sense Dec 22 '16 at 16:03
  • @YourCommonSense ok, thank you – peace_love Dec 22 '16 at 16:30

1 Answers1

0
$stmt1 = $db->prepare('INSERT INTO people (name) values(:name)');
$stmt1->execute(array(':name' => $_POST['name'],));

$stmt2 = $db->prepare("UPDATE animals SET name =? WHERE id=LAST_INSERT_ID();");
$stmt2->bindParams(1,$name);
$stmt2->execute();

//did you now that you can do this
//$name='newvalue';
//$stmt2->execute();
//$name='nextvalue';
//$stmt2->execute();
JustOnUnderMillions
  • 3,741
  • 9
  • 12