1

I have table like below enter image description here

My query is :

INSERT INTO geo_layer (map_id, user_id, type, lat, lng, more_info) VALUES ('".$geo_id."','".$user_id."','".$type."','".$latln_0."','".$latlng_1."','".$more_info."') ON DUPLICATE KEY UPDATE map_id = '".$geo_id."', user_id = '".$user_id."', type = '".$type."', lat = '".$latln_0."', lat = '".$latlng_1."', more_info = '".$more_info."' WHERE map_id = '".$geo_id."' AND user_id = '".$user_id."'";

Query Output is:

INSERT INTO geo_layer (map_id, user_id, type, lat, lng, more_info) VALUES ('82','1','Point','88.409042358398','22.544903493838','') ON DUPLICATE KEY UPDATE map_id = '82', user_id = '1', type = 'Point', lat = '88.409042358398', lat = '22.544903493838', more_info = '' WHERE map_id = '82' AND user_id = '1'INSERT INTO geo_layer (map_id, user_id, type, lat, lng, more_info) VALUES ('82','1','Point','88.467063903809','22.564244372033','') ON DUPLICATE KEY UPDATE map_id = '82', user_id = '1', type = 'Point', lat = '88.467063903809', lat = '22.564244372033', more_info = '' WHERE map_id = '82' AND user_id = '1'INSERT INTO geo_layer (map_id, user_id, type, lat, lng, more_info) VALUES ('82','1','Point','88.401719927788','22.506530781509','') ON DUPLICATE KEY UPDATE map_id = '82', user_id = '1', type = 'Point', lat = '88.401719927788', lat = '22.506530781509', more_info = '' WHERE map_id = '82' AND user_id = '1'

What I want is: I like to update / replace existing data of multiple columns and if new data it will insert. but I don’t have access to unique id like primary key. data is coming from a array without any previous information.

Problem is except “layer_id” (auto increment) nothing is unique and I do not know what is layer_id before it run my query.

Solution I am thinking is check every row for existing value then insert if it new or update /replace previous or do both.

Please let me know if more details require.

Kuntal Parbat
  • 159
  • 1
  • 2
  • 12

0 Answers0