Im currently writing a php loader to my mySQL database . The user enter user|password and write the database table he want to insert to , and select a region from
The form action goes to database.php to do the insert using POST
my code in database.php is:
$region = $_POST["region"];
$tablename = $_POST["tableName"];
$accounts = explode(PHP_EOL, $_POST["accountlist"]);
$quantity = sizeof($accounts);
// Batch size, calc iterations
$batchSize = 500;
for($idx=0;$idx*$batchSize < $quantity; $idx++){
$accountsPartial = array_slice($accounts, $idx*$batchSize, $batchSize);
// Prepare the Query
$query = "INSERT INTO $tablename (username, password, region) VALUES (?, ?, ?)";
// Create the multiple value placeholder
$db = $db->prepare($query);
foreach($accountsPartial as $item){
$i = 1;
list($user, $pass) = explode("|", $item);
$db->bindValue($i++, trim($user));
$db->bindValue($i++, trim($pass));
$db->bindValue($i++, trim(strtoupper($region)));
$db->execute();
}
}
My problem is when I sent more then 500 Values , only 500 accounts getting Insereted. I would love to get some help and suggestions how to solve this issue Thanks in advance!
fordefeats the purpose of preparing statements in the first place. But at least you're doing it inside theforeach, so there's that, at least. Plus. why batchsize at all? If you want to insert all of the records, then just split your array and loop on the entire thing.