Your code may work, but here’s what’s technically wrong with the code, that’s wasting typing time when you wrote it and is wasting processing time and memory when it runs -
- The $rows array is not used and should have been $ids. The $ids array is not being initialized. If you happened to use $ids prior to the posted code, you will end up with the wrong/too much data being inserted.
-
You are using a prepared query for the SELECT query, but there’s no external/unknown values being supplied to the sql query statement. Just use the ->query() method to execute this query.
-
You are looping to fetch all the rows from the query. Just use the ->fetchAll() method.
-
You are specifying the fetch mode in the fetch statement. If you set the default fetch mode to assoc when you make the database connection, you won’t have to keep listing it in each fetch statement in your code.
-
Here’s the biggest problem. You are repeatedly preparing the query and binding the input data for the INSERT query. One of the main points of using a prepared query is to save the time it takes to communicate the sql query statement between php and the database server and the time it takes on the database server to parse and plan the execution of the query. As I already posted -
-
There’s no need to use bindParam() or bindValue(). it’s implied you need to use bindParm() when executing a query more than once, but this is incorrect. Just supply an array of the input data to the ->execute() method call.
-
I’m surprised your code does work, since you are using both bindParam() and supplying an array of the input data to the ->execute() method call.