import into mysql table
Posted: Mon Jan 24, 2005 2:57 am
Hello Everyone
I am trying to import data into mysql table. I am able to upload data using the following code
<?
$uploaddir = 'uploads/';
$uploadfile = $uploaddir. $_FILES['file']['name'];
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
file://echo "Uploaded Successfully";
} else {
file://echo "!!";
}
$fcontents = file ('./uploads/product.txt');
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i+1]);
$arr = explode("\t", $line);
$sql = "insert into product values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
file://echo mysql_error() ."<br>\n";
}
}
?>
my problem is i am able to add new records into the database whch where not there previously leaving the existing products
but how do i
a)replace existing files- all records in the product table which contain product code that matches those found in the records in the uploading file
b)update existng files - where selected fileds in the table needs updating with new information and all the other information in the table remain same.
Any help highly appreciated
I am trying to import data into mysql table. I am able to upload data using the following code
<?
$uploaddir = 'uploads/';
$uploadfile = $uploaddir. $_FILES['file']['name'];
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
file://echo "Uploaded Successfully";
} else {
file://echo "!!";
}
$fcontents = file ('./uploads/product.txt');
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i+1]);
$arr = explode("\t", $line);
$sql = "insert into product values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
file://echo mysql_error() ."<br>\n";
}
}
?>
my problem is i am able to add new records into the database whch where not there previously leaving the existing products
but how do i
a)replace existing files- all records in the product table which contain product code that matches those found in the records in the uploading file
b)update existng files - where selected fileds in the table needs updating with new information and all the other information in the table remain same.
Any help highly appreciated