Jump to content

Duplicate entry - for key, issues uploading to sql database


ash992

Recommended Posts

hey guys, I'm getting a problem uploading my csv file to mysql database, the code on the top half just makes sure that I don't import data that I don't want into the database, numbers that are too high or too low on certain rows, and the second half is importing the array that I create from the csv file into the database however I'm getting the following error

 

 

 

Duplicate entry 'Array' for key 'strProductCode'

 

Now I've never had this error before and I'm not sure what's causing it so any help would be very much appreciated

$data = array();
if (($handle = fopen("stock.csv", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
// only add rows to the array where the 4th column value is greater than or equal to 10
if(($row[3] >= 10 && $row[4] >= 5) OR ($row[3] >= 0 AND $row[4] > 5)){
if($row[4] < 1000){
$data[] = $row;
}
}
}


foreach ($data as &$value) {


mysql_query("INSERT INTO tblProductData 
(intProductDataId, strProductCode, strProductName, strProductDesc, strProductStock, strProductCost, dtmDiscontinued, dtmAdded, stmTimestamp) 
VALUES(null, '$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]', 'time', 'added', 'time') ") 
or die(mysql_error());  


}
}

Thanks in advance guys! if you have any clue at all I'd love to hear it.

 

 

oh btw, I've just taken a look in the database and this is what is imported when I run the script 

 

error.png

 

so possibly the reason for it saying there's a duplicate is becuase I'm uploading the array incorrectly? so it's just trying to upload the wrong values everytime to the same row? :S 

 

if you know anything about this it would be great, thanks again in advance! 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.