How to use inserting multiple rows (bulk inserting) at a time in php (dynamic rows)

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 -

  1. 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.
  1. 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.

  2. You are looping to fetch all the rows from the query. Just use the ->fetchAll() method.

  3. 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.

  4. 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 -

  1. 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.

  2. 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.

Ignoring what data processing you are doing in your example, and just from a code/query standpoint, this is the equivalent code, taking into account the points that have been made in the replies -

// produce, execute, and fetch a set of data for a non-prepared SELECT query
$sql = "SELECT email FROM parents_details";
$stmt = $conn->query($sql);
$rows = $stmt->fetchAll(); // note: $rows will either be an array of the row(s) of data or an empty array, there's no need to initialize it when using this code

// produce, prepare, and execute a prepared INSERT query, multiple times using a loop
$sql = "INSERT INTO `student_reg`(`student_name`) VALUES (?)";
$stmt = $conn->prepare($sql);
foreach($rows as $row)
{
	$stmt->execute([$row['email']]);
}

@phdr, that is overkill as well. Since all the values are already in the table…

INSERT INTO `student_reg`(`student_name`) SELECT email FROM parents_details

But, like I said before, this will create duplicates.

Except that the OP has a html form situated between those two processes in reality.

He could always add a where clause to the above, but in the code, it doesn’t display anything from a form or take anything in from the form.

This is one of those times where it is a stretch to understand what is needed, when they can’t communicate well enough to show the issue.

Sponsor our Newsletter | Privacy Policy | Terms of Service