Read from txt file and update or insert datas to database???
Posted: Thu Apr 08, 2004 1:46 am
Hey guyz, I'm still new to this so take it easy on me..
Ok here is my problem.. here is my code below
<?php
//testing read csv file and sorting it out.
$filename = "test.txt";
dbproducts();
if (!($fp = fopen($filename,"r")))
exit("Unable to open $filename.");
while (!feof($fp))
{
$i=$i+1;
$buffer = fgets($fp,1024);
$buffer = trim($buffer);
$pos = strpos($buffer, ",");
$barcode = substr($buffer, 0, $pos);
$buffer = trim(substr($buffer, $pos+1));
$pos = strpos($buffer, ",");
$name = substr($buffer, 0, $pos);
$buffer=trim(substr($buffer, $pos+1));
$price = $buffer;
$name = trim($name);
$pos = strpos($name, " ");
$name = substr($name, $pos);
if (mysql_query("SELECT fld_barcode FROM tbl_products WHERE fld_barcode=$barcode"))
{
mysql_query("UPDATE tbl_products SET fld_barcode='$barcode', fld_name='$name', fld_price='$price' WHERE fld_barcode=$barcode");
$jupdate=$jupdate+1;
} else {
mysql_query("INSERT INTO tbl_products (fld_barcode, fld_name, fld_price) VALUES ('$barcode', '$name', '$price')");
$jinsert=$jinsert+1;
}
}
fclose ($fp);
print ("$i products in total<br> $jupdate products update<br> $jinsert products inserted");
?>
my database fld_barcode is varchar(25).
I think the problem is the if statement.
if (mysql_query("SELECT fld_barcode FROM tbl_products WHERE fld_barcode=$barcode"))
{
the problem is if the barcode match or not in the database it will always end up the same. I change barcode to see the result and still the same thing.
What happening is if barcode has int only then it will do update goes into if statement.
but if the barcode has a char in it then it will do insert goes to else statement.
no matter if $barcode match or not with fld_barcode in the database.
What i'm doing wrong.
Ok here is my problem.. here is my code below
<?php
//testing read csv file and sorting it out.
$filename = "test.txt";
dbproducts();
if (!($fp = fopen($filename,"r")))
exit("Unable to open $filename.");
while (!feof($fp))
{
$i=$i+1;
$buffer = fgets($fp,1024);
$buffer = trim($buffer);
$pos = strpos($buffer, ",");
$barcode = substr($buffer, 0, $pos);
$buffer = trim(substr($buffer, $pos+1));
$pos = strpos($buffer, ",");
$name = substr($buffer, 0, $pos);
$buffer=trim(substr($buffer, $pos+1));
$price = $buffer;
$name = trim($name);
$pos = strpos($name, " ");
$name = substr($name, $pos);
if (mysql_query("SELECT fld_barcode FROM tbl_products WHERE fld_barcode=$barcode"))
{
mysql_query("UPDATE tbl_products SET fld_barcode='$barcode', fld_name='$name', fld_price='$price' WHERE fld_barcode=$barcode");
$jupdate=$jupdate+1;
} else {
mysql_query("INSERT INTO tbl_products (fld_barcode, fld_name, fld_price) VALUES ('$barcode', '$name', '$price')");
$jinsert=$jinsert+1;
}
}
fclose ($fp);
print ("$i products in total<br> $jupdate products update<br> $jinsert products inserted");
?>
my database fld_barcode is varchar(25).
I think the problem is the if statement.
if (mysql_query("SELECT fld_barcode FROM tbl_products WHERE fld_barcode=$barcode"))
{
the problem is if the barcode match or not in the database it will always end up the same. I change barcode to see the result and still the same thing.
What happening is if barcode has int only then it will do update goes into if statement.
but if the barcode has a char in it then it will do insert goes to else statement.
no matter if $barcode match or not with fld_barcode in the database.
What i'm doing wrong.