How to do an update temporary tables in a foreach loop?

Walk around in a loop a simple array in the process want to create temporary tables by id to take from them additional data, and then to update and insert into database.

But the cycle is triggered once. What am I doing wrong?

foreach ($array as $key => $value) {

 $id = $key;
 $text = $value;

 $query = "SET @last_id = LAST_INSERT_ID(); ";
 $query .= "CREATE TEMPORARY TABLE foo AS SELECT * FROM texts WHERE id = $id; ";
 $query .= "UPDATE foo SET id = @last_id, html_text = '$text'; ";
 $query .= "INSERT INTO texts SELECT * FROM foo; ";
 $query .= "DROP TABLE foo; ";
 mysqli_multi_query($link, $query); 

 } 
 mysqli_close($link);
April 3rd 20 at 17:16
1 answer
April 3rd 20 at 17:18
Solution
It's a horror, not code. This is all wrong.

You do not need any temporary tables, no need multi_query (this function is actually never needed). The queries need to execute using prepared statements.

The request need only one.
$sql = "INSERT INTO texts SELECT null, the primary objective of BCC, ballon_no, text_lang, font_size, html_left, 
 html_top, html_width, html_height, ?, rotate, text_align, aspect_ratio,
 position_ratio, calc_width, calc_left, font_color, line_height, font_type 
 FROM texts WHERE id=?";
$stmt = $link->prepare($sql);
$stmt->bind_param("ss", $text, $id);
foreach ($array as $id => $text) {
$stmt->execute();
}
Error: Column count doesn't match value count at row 1. - annalise64 commented on April 3rd 20 at 17:21
So it is necessary to specify the match fields.
plus I added a null at the front, after I sent my response. - jovany98 commented on April 3rd 20 at 17:24
Yes, it worked with null. Thank you! - annalise64 commented on April 3rd 20 at 17:27

Find more questions by tags PHPSQL