I’m having a bit of a problem when it comes to a single mySQL script; specifically, mySQL keeps giving me this error:
MySQL Error: Duplicate entry '39835a3f63f222c9bf51d9dd471b90bc' for key 'PRIMARY'
It’s pretty weird cause the primary key in question is a MD5 generated hash based off of the time() function of php, so there shouldn’t be any issues.
This is the php code that inserts into the table:
INSERT INTO log
(hailID, timestamp, lat, lng, phone, device, method, serviceType, email, address, languageID, ipaddress)
VALUES
(MD5('" . $time . $data['phone'] . "'),
'$time',
'".$data['lat']."',
'".$data['lng']."',
'".$data['phone']."',
'".$data['device']."',
'".$data['method']."',
'".$data['serviceType']."',
'".$data['email']."',
'".$data['address']."',
'".$data['languageID']."',
'".$_SERVER["REMOTE_ADDR"]."'
$time in this case, is just a variable holding the value from the time() function. Funnily enough, this problem isn’t very frequent, and it appears to be random (at least as far as I can tell). And yes, all the data is sanitized using mysqli_escape_string() before it goes into the $data array. I’ve also checked my table using CHECK TABLE and no errors have appeared either.
Any ideas?
Thanks
If 2 entries are logged within the same second they will have the same hash value. This will happen more frequently than you think on even a fairly low-use system. I would suggest:
DATETIMEorTIMESTAMPusingNOW(). You’ll actually have a sortable table.INTwithAUTO_INCREMENTmicrotime()function.Think about it, even if you stored the ID as an 8-byte
BIGINTit’s still 4x smaller than theVARCHAR(32)that you need to store an MD5 hash.