I am working on application in which i need to upload data through a csv file and then save record into mysql table. I am doing this in php. Till now i have right this code.
<?php
//date_default_timezone_set("Asia/Karachi");
//echo "The time is " . date("Y-m-d h:i:sa");
$DateTime=date("Y-m-d h:i:sa");
$FilePath=$_POST['img'];
$ID=$_POST['ID'];
$Query_String2="";
// echo $FilePath;
$row = 1;
if(isset($FilePath))
{
// echo "ID is =".$ID;
$Query_String1 = "INSERT into ap_form_$ID VALUES";
if (($handle = fopen($FilePath, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
if($row!=1)
$Query_String2=$Query_String2.",";
$Query_String2= $Query_String2."("."NULL".","."'".$DateTime."'".","."NULL".","."'127.0.0.1'".","."1".","."NULL";
$num = count($data);
$row++;
$master = array();
for ($c=0; $c < $num; $c++)
{
if($c==0)
$Query_String2=$Query_String2.","."'".$data[$c]."'";
else
$Query_String2=$Query_String2.","."'".$data[$c]."'";
//$master[$c]= $data[$c];
}
$Query_String2=$Query_String2.")";
}
$Final_Query=$Query_String1.$Query_String2;
echo "Final Query =".$Final_Query;
$connection = mysqli_connect("localhost", "root","","form_db");
$result = mysqli_query($connection,$Final_Query) or mysql_error();
if($result > 0)
{
echo "
<script type=\"text/javascript\">
alert('Record has been inserted into the form');
</script>
";
}
else
{
echo "
<script type=\"text/javascript\">
alert('I am sorry there is some problem');
</script>
";
}
fclose($handle);
What i am doing in this code is, i read data from CSV file and then make a long query string like "Insert into table value ("bla, bla ,bal),("bla, bla, bla)" This code work fine for the small data just like when i have data upto 1000 in the csv file But when data is more the 1000 , the process stuck and i am not be able to save data in mysql.In my application i need to upload 50000 at minimum. Any idea or solution to solve this problem.