Well I think you might be suffering from a little known problem with regards to updating and how PHP mysql_affected_rows responds. Here is the questionable part...
"When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query. " -
PHP.net for mysql_affected_rows functionSo lets say you have one record with the session id, time start and value. The process then fires again for the _write where it determines that the session id and time are the same as the record already there. It will execute the update, but not update that existing record.
This results in the update command being executed so mysql_query will return true avoiding your first if statement, but since no records were actually updated it would return "0" making second if statement (where you check mysql_affected_rows) false. Thus it bypasses both your if statements and goes straight to the insert.
Now this would also explain why you see it only sometimes because as soon as the time changes, it will update the one record just fine and work as expected.
This is a general theory so what I would do to see if it fixes it is that instead of just running the update, first pull the id of that record (assuming your records have an id field that increments... if not you should make sure you have one in) using a select statement, if you found a record, issue the update on that record. If you didn't find the record, issue the insert.
Return true if an update or insert was done, false otherwise.
That should scoot you past this issue.
This post has been edited by Martyr2: 6 Jul, 2008 - 01:42 PM