1st statement: Select multiple data from table1 and copy them to
table2.
You will need to use INSERT INTO SELECT which you did well.
$query ="INSERT INTO table2 (name, `number`, price, quantity, sumPrice, supplier, customer)
SELECT name, `number`, price, quantity, sumPrice, supplier, customer
FROM table1
WHERE table1_id = '".$id."'";
2nd statement: Save a variable to table2.
One thing you did not notice is that after WHERE table1_id = '".$id."'";
you terminated $query
therefore this never runs `
"INSERT INTO table2(adoptedBy)
VALUES ('$name')";
what you can do is to use mysqli_multi_query();
Then you query will be something like :
<?php
$query = "INSERT INTO table2 (name, `number`, price, quantity, sumPrice, supplier, customer)
SELECT name, `number`, price, quantity, sumPrice, supplier, customer
FROM table1
WHERE table1_id = '" . $id . "';";
$query .= "UPDATE table2 SET adoptedBy =".$name." where id = LAST_INSERT_ID()";
$result = mysqli_multi_query($db_link, $query);
if ($result) {
echo "success";
} else {
echo "Error : " . mysqli_error($db_link);
}
?>
Hope this helps.
NB: I advice that you start learning doing sql queries using prepared
statements whether you are using mysqli prepared or pdo
prepared statements
Edit :
Alternative when the above suggestion does not work,
You may update the LAST_INSERT_ID() when the first query run successfully get that last inserted id then do an update on that id
<?php
$query = "INSERT INTO table2 (name, `number`, price, quantity, sumPrice, supplier, customer)
SELECT name, `number`, price, quantity, sumPrice, supplier, customer
FROM table1
WHERE table1_id = '" . $id . "';";
$result = mysqli_query($db_link, $query);
if ($result) {
$query = "UPDATE table2 SET adoptedBy ='".$name."' WHERE id = LAST_INSERT_ID();";
if(mysqli_query($db_link,$query)){
echo "success";
}else{
echo "update failed".mysqli_error($db_link);
}
} else {
echo "Error : " . mysqli_error($db_link);
}
?>