Jump to content

Recommended Posts

Hello guys, my ON DUPLICATE KEY UPDATE  doesn't work correctely; 

 

data are uploaded every day AND many times for updates, from CSV files

 

the CSV files look is 

 

csv file of yesterday:

1  " date"               , "  route  ",
2  " 2013.06.10"         , "    A    ",
3  " "                   , "    D    ",
4  " "                   , "    G    ",

csv file of today:

1  " date"               , "  route  ",
2  " 2013.06.11"         , "    C    ",
3  " "                   , "    D    ", 
4  " "                   , "    M    ",
5  " "                   , "    Z    ",

My table in MySQL must be like this :

 

id |     date             | route |    

----------------------------

1 |    2013.06.10   |    A    |

2 |    2013.06.10   |    D    |          // data of day : 2013.06.10

3 |    2013.06.10   |    G    |

4 |    2013.06.11   |    C    |

5 |    2013.06.11   |    D    |

6 |    2013.06.11   |    M    |

7 |    2013.06.11   |    Z    |

 

there are no unique columns, are are duplicated for each record, and route might have duplicate entries

 

For each day there are insert and update,

if ($date == $today OR $date==" ")
{
​$sql = mysql_query ("INSERT INTO my_table 
(date, route) VALUES ('$date' , '$route') ON DUPLICATE KEY UPDATE date='$date', route ='$route' ");
}

I can insert and update any columns but here are the issue :

 

2)  when I refrech my browser, data are loader again as much as I refrech my web-page, lines are duplicated 

 

1 |    2013.06.10   |    A    |

2 |    2013.06.10   |    D    |          

3 |    2013.06.10   |    G    |

4 |    2013.06.10   |    A    |          // same data  for 1st refresh

5 |    2013.06.10   |    D    |          

6 |    2013.06.10   |    G    |         

1 |    2013.06.10   |    A    |          // same data  for 2nd refresh

2 |    2013.06.10   |    D    |          

 

3 |    2013.06.10   |    G    |

Edited by avia767
Link to comment
https://forums.phpfreaks.com/topic/279050-on-duplicate-key-update-pb/
Share on other sites

Make the id column auto_increment primary key.

 

Make (date, route) a unique key.

my table columns 

 

- id        [iNT (11), AUTO INCREMENT] PRIMARY KEY

- date    [DATE]

- route   [VARCHAR (9)]

 

the entries for each columns could be duplicated, because every day I make instert and update. 

 

once data are correctly uploaded must be like this:

 

id |     date             | route |    

----------------------------

1 |    2013.06.10   |    A    |

2 |    2013.06.10   |    D    |          // data of day : 2013.06.10

3 |    2013.06.10   |    G    |

4 |    2013.06.11   |    C    |

5 |    2013.06.11   |    C    |

6 |    2013.06.11   |    M    |

7 |    2013.06.11   |    Z    |

 

As you can see, date and route are duplicated for each day depending on data uploaded from my csv file.

I can insert and update any columns but here are the issue :

 

2)  when I refrech my browser, data are loader again as much as I refrech my web-page, lines are duplicated 

 

1 |    2013.06.10   |    A    |

2 |    2013.06.10   |    D    |          

3 |    2013.06.10   |    G    |

4 |    2013.06.10   |    A    |          // same data  for 1st refresh

5 |    2013.06.10   |    D    |          

6 |    2013.06.10   |    G    |         

1 |    2013.06.10   |    A    |          // same data  for 2nd refresh

2 |    2013.06.10   |    D    |          

 

3 |    2013.06.10   |    G    |

 

 

it doesn't work, I got

 

#1062 - Duplicate entry '2013-06-11-6233' for key 'date'

 

because 'date' and 'route' can be duplicated, not as Unique !! 

 

First you complain about duplicates then you say they can be duplicated. I'm out of here.

  • 3 weeks later...

hi every body, my pb is solved, 

 

I have this SQL statement 

 

 

$sql_insert = mysql_query (
"INSERT INTO test (
date, 
route, 
timeDEPARTURE, 
timeACTUALISED, 
delay
) 
VALUES (
'$datecsv_format', 
'$routeCSV', 
'$depCSV', 
'$actCSV', 
'$delayCSV' 
) 
ON DUPLICATE KEY UPDATE 
date ='$datecsv_format',
route = '$routeCSV', 
timeDEPARTURE = '$depCSV', 
timeACTUALISED = '$actCSV', 
delay = IF('$actCSV' == '00:00:00', '$delaycsv', timediff ( timeACTUALISED , timeDEPARTURE ))
");

my probleme I'am sur is in the the IF condition here

delay = IF('$actCSV' == '00:00:00', '$delaycsv', timediff( timeACTUALISED , timeDEPARTURE ))

any help ?

 

 

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.