csv file upload and update mysql db
Posted
by
Indra
on Stack Overflow
See other posts from Stack Overflow
or by Indra
Published on 2013-11-02T09:42:13Z
Indexed on
2013/11/02
9:53 UTC
Read the original article
Hit count: 197
I am very to new to php. i am using the following code to open a csv file and update my database. i need to check the value of first row-first column of the csv file. if it is matching "some text 1", then i need to run code1, if it is "some text 2", run code2, else code3. I can use if else condition but since i am using while loop It fails. Can anyone help me.
$handle = fopen($file_tmp,"r");
while(($fileop = fgetcsv($handle,",")) !== false)
{
// I need to check here
$companycode = mysql_real_escape_string($fileop[0]);
$Item = mysql_real_escape_string($fileop[3]);
$pack = preg_replace('/[^A-Za-z0-9\. -]/', '', $fileop[4]);
$lastmonth = mysql_real_escape_string($fileop[5]);
$ltlmonth = mysql_real_escape_string($fileop[6]);
$op = mysql_real_escape_string($fileop[9]);
$pur = mysql_real_escape_string($fileop[10]);
$sale = mysql_real_escape_string($fileop[12]);
$bal = mysql_real_escape_string($fileop[17]);
$bval = mysql_real_escape_string($fileop[18]);
$sval = mysql_real_escape_string($fileop[19]);
$sq1 = mysql_query("INSERT INTO `sas` (companycode,Item,pack,lastmonth,ltlmonth,op,pur,sale,bal,bval,sval) VALUES ('$companycode','$Item','$pack','$lastmonth','$ltlmonth','$op','$pur','$sale','$bal','$bval','$sval')");
}
© Stack Overflow or respective owner