Jump to content

Duplicate entry - for key, issues uploading to sql database


ash992
Go to solution Solved by Jacques1,

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.

 

 

Link to comment
Share on other sites

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! 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.