MySQL Forums
Forum List  »  PHP

CSV into mysql - How do I overwright existing data ?
Posted by: Daffy Duck
Date: November 10, 2005 06:35PM

Hi im inserting some data from a csv file into mysql using the following...

<?
$link = mysql_connect("localhost", "u", "p") or die("Could not connect: ".mysql_error());

$db = mysql_select_db("dac") or die(mysql_error());


$handle = fopen ("file.csv","r");

while ($data = fgetcsv ($handle, 1000, ",")) {

$query = "INSERT INTO client(`Name`, `Road`, `Town`, `County`, `Country`, `PostCode`, `Status`) VALUES('"
.$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."')";
$result = mysql_query($query) or die("Invalid query: " . mysql_error().__LINE__.__FILE__);

}
fclose ($handle);
?>

Which works ok apart form I get an error..

PHP Notice: Undefined offset: 2 in /var/www/html/db/db3.php on line 11
PHP Notice: Undefined offset: 3 in /var/www/html/db/db3.php on line 11
PHP Notice: Undefined offset: 4 in /var/www/html/db/db3.php on line 11
PHP Notice: Undefined offset: 5 in /var/www/html/db/db3.php on line 11
PHP Notice: Undefined offset: 6 in /var/www/html/db/db3.php on line 11

If anyone knows where I messed up would be grateful..

But my main problem is that when I need to insert an updated CSV file with changes ie same Name field but different address it creates a new entry for that person (so I end up with 2 entries for same name)

How can I overwright the 1st entry with the updated entry?

Thanks.



Edited 1 time(s). Last edit at 11/10/2005 06:36PM by Daffy Duck.

Options: ReplyQuote


Subject
Written By
Posted
CSV into mysql - How do I overwright existing data ?
November 10, 2005 06:35PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.