import into mysql table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
achus
Forum Newbie
Posts: 1
Joined: Mon Jan 24, 2005 2:12 am

import into mysql table

Post by achus »

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
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Post by thomas777neo »

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:

Code: Select all

//Get X

$product_code = $get_product&#1111;$i]&#1111;"product_code"];

$check = $object_var->check_product($product_code);

//check_product is merely a function in class $object_var.

if ($check == true)
&#123;
    update data in Y
&#125;
  else
  &#123;
     insert data into Y
  &#125;
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.
Post Reply