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
import into mysql table
Moderator: General Moderators
- thomas777neo
- Forum Contributor
- Posts: 214
- Joined: Mon Mar 10, 2003 6:12 am
- Location: Johannesburg,South Africa
Looks like you are trying to import a comma seperated file.
Are you using mysql?
Anyway, I think you should import the data as usual into table X. Then create another table Y with the same structure as X.
Once you have imported the data into X. Loop through the records and compare it to Y.
Concept:
So X is just a temporary table, Y is your master table.
You can also TRUNCATE X after you have done the checking process. So that the next time you compare the data, it would be a fresh import of data.
Are you using mysql?
Anyway, I think you should import the data as usual into table X. Then create another table Y with the same structure as X.
Once you have imported the data into X. Loop through the records and compare it to Y.
Concept:
Code: Select all
//Get X
$product_code = $get_productї$i]ї"product_code"];
$check = $object_var->check_product($product_code);
//check_product is merely a function in class $object_var.
if ($check == true)
{
update data in Y
}
else
{
insert data into Y
}You can also TRUNCATE X after you have done the checking process. So that the next time you compare the data, it would be a fresh import of data.