I've got the below code snippet which works fine when I try to upload a CSV file with relatively smaller (~500) entries. However when the entries get larger (I tried 20,000) it triggers the following error.
You have an error in your sql syntax check the manual that corresponds to your mysql server version for the right syntax to use near 'S FANCY-KNDY0046','1298','32321','QE4224','2')' at line 1
My INSERT into
looks like this: (Haven't really thought of sanitizing the input here yet but your thoughts would come in handy too!)
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$sql = "INSERT into universe (`zone`, `area`, `sub`) values('$emapData[0]','$emapData[1]','$emapData[2]')";
$result = mysqli_query($conn, $sql);
if(!$result )
{
echo "ERROR:". mysqli_error($conn); // I included this to see the error message.
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"index.php\"
</script>";
}
}
fclose($file);
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"index.php\"
</script>";
mysqli_close($conn);
}
Appreciate if someone can point me if I'm doing something wrong here! Thank you.