Answer the question
In order to leave comments, you need to log in
What is the correct way to update temporary tables in a foreach loop?
I go around a simple array in a loop, in the process I want to create temporary tables by id in order to take additional data from them, then update and insert them into the database.
But the loop only works 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);
Answer the question
In order to leave comments, you need to log in
This is some kind of horror, not a code. Everything is wrong here.
No temporary tables needed, no multi_query needed (this function is never needed at all). Queries must be executed using prepared expressions. Only one
request is needed .
$sql = "INSERT INTO texts SELECT null, page_id, 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();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question