Background: This is the first time I've posted a question here, but I have learned a lot from reading others' questions and answers and I really appreciate all of your help! I have spent several hours researching this problem and can't find a solution that works for me. The example below is from the MySQL website and thus far, seems to be the most reliable.
Goal: Ultimately, I want to link the user id from the registration form (foo for now) database to other databases (foo2 for now). I THINK I understand the INSERT INTO and the LAST_INSERT_ID and have tried many example I've read here. Assuming my code for that is correct (which it might not be), I think my question is regarding the syntax and code outside of that area (the entire function createRows). Do I need two $query or just the one? Many examples I've seen don't show the query parts or code beyond just the INSERT INTO statements and that's what I need to see.
Problem: I run this code and don't get any errors. However, data is only going into table foo2, completely skipping/bypassing table foo, which remains completely empty.
function createRows(){
if (isset($_POST['submit'])) {
global $connection;
$text = $_POST['text'];
$query = "INSERT INTO foo (id,text) VALUES(NULL,'$text')";
$query = "INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),'$text')";
$result = mysqli_query($connection, $query);
if (!$result) {
die('Query FAILED' . mysqli_error($connection));
} else {
echo "Success! New record created.";
}
}
}