I am working on a app in CI, I have a model that inserts the image paths from an upload form into the database.
This code works 100% correct in my localhost environment, but when running it on the remote server it fails with
Error Number: 1062
Duplicate entry '89-ci/flatfee/uploads/37' for key 'PRIMARY'
The idea is to use an image_map_table that maps a listingid to an image_path, so the listingid has to be repeated several times over in the table for each image path, hence the duplicate entry. Below is the function in the model:
function insert_paths($params, $transID){
$lid = $this->getListingID($transID);
if($lid != false) {
$i = 0;
foreach ($params['image_paths'] as $value) {
$sql = "INSERT INTO listing_image_map(idlisting, image_path) VALUES(".$lid.", '".$value."')";
$q = $this->db->query($sql);
$i++;
}
if(sizeof($params['image_paths']) == $i ){
return true;
}
} else {
return false;
}
}
$params is an array, passed in from the controller, of all the image paths from the upload form. I understand the error, and why it is happening at a basic level, but a more detailed explanation as to why this works on localhost and not on remote server would be nice, and a possible resolution as well.
Thanks.
EDIT : I found the problem thanks to Ashwini Dhekane. The file paths are created from an array, so the default file path on the server is longer in its structure then on my local environment, so the indices used to create the folder structure had to be increased for the remote server and decreased in the local environment (so right now I have two sets of that code that I keep commenting and un-commenting depending on which server I am working in, bit of a hassle, but I only have to work on this part for a few more days.)
Thanks Ashwini, your post made me look in the right area, so I’ll mark it as correct.
'89-ci/flatfee/uploads/37'seems to be the path of the upload file and error shows that the path is theprimary keycolumn. You cannot have any two rows having same value in the primary key column.If you are trying to upload a SQL dump, then you cannot do it; the keys will always collide. In this case you will have to write a customized script.