您正在使用外部查询的所有结果集,而未在内部循环中使用它。
下一个问题也是对两个查询使用相同的变量,因为当您运行内部查询时,它将破坏外部外观的结果集
I do have to mention that you are open to SQL Injection Attack.
Even if you are escaping inputs, its not safe!
You should consider using prepared parameterized statements in either the MYSQLI_ or PDO API's instead of concatenated values