|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
Hi Jess. I thought that your system believed I was trying SQL injection because my search term used "replace" but I changed that to "change" as in "change brake pad" and still got an error like this one.
|
|
|
UTC
quote
ScooterWoman9988 wrote: Hi Jess. I thought that your system believed I was trying SQL injection because my search term used "replace" but I changed that to "change" as in "change brake pad" and still got an error like this one. |
|
|
UTC
quote
Were there any other parameters you used? Posts vs Threads? I tried with the same search string and it seemed to work, but I could very well be missing something.
|
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
Nope. I just put in "replace brake pad" as the search term and hit enter. Got the error and changed "replace brake pad" to "change brake pad" and tried again. Maybe it has resolved itself.
EDIT: Yup. Seems to be working now. I have no idea what happened. |
|
|
UTC
quote
ScooterWoman9988 wrote: EDIT: Yup. Seems to be working now. I have no idea what happened. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
|
|
UTC
quote
The mystery deepens: I went and looked at the code that threw this error. The duplicate key that it's complaining about is generated -- immediately before the SQL operation -- by generating a random number. And it does appear to be properly seeded.
So I am more puzzled than ever as to how that could have happened. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
It's not generating a GUID? That would be far less likely to ever generate a dupe than a random number generator.
|
|
|
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
I'm surprised it's not using an auto incrementing primary key field if it wants to use numbers for the primary key. No chance of duplication then.
Is there any way to change it to auto incrementing and remove the primary key from the INSERT command? |
|
|
UTC
quote
ScooterWoman9988 wrote: I'm surprised it's not using an auto incrementing primary key field if it wants to use numbers for the primary key. No chance of duplication then. These days, of course it makes sense to use those capabilities, and they are widely available with virtually every database. |
|
|
UTC
quote
Okay, digging a little deeper: it appears the strategy goes something like this:
1. Generate pseudorandom number, seeded with microtime() 2. If there's already a set of search results associated with the session_id (i.e. that user's session cookie) then update the database with the new search results and set the search_id to the new random number 3. If there ISN'T a set of search results associated with the session, insert using the search_id field (which must be unique) So that explains why auto-increment isn't used. While search_id must be unique, it is secondary to the session_id. My question is: why have search_id at all? Probably because session_id historically wasn't very reliable at the dawn of web apps. But still. This seems like a bad strategy. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
|
|
UTC
quote
ScooterWoman9988 wrote: Got it. It's not a big problem, anyway. It might not happen again for years. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
I don't think you're missing anything. If so wouldn't it have shown up a long time ago or happen on a regular basis? Seems to me the random number generator simply generated a couple of numbers that had already been used.
|
|
|
UTC
quote
ScooterWoman9988 wrote: I don't think you're missing anything. If so wouldn't it have shown up a long time ago or happen on a regular basis? Seems to me the random number generator simply generated a couple of numbers that had already been used. That feels like a hole in the logic. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
So you're saying the search results are held in a temp table of some sort? I thought they were INSERTed into a permanent table that has been holding all the search results since inception of the site.
OK, then, yeah. There's something off with the logic but given that it can't easily be duplicated is it worth taking a few years off your life trying to track it down? I wouldn't bother and would just chalk it up to one of those things that happens once every ten years or so. |
|
|
UTC
quote
This might have something to do with it. This is the test to see if the update worked:
if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() ) The first clause is the standard form used throughout this forum software: execute the SQL command and check the result. The second clause is much less common -- and I'm not sure why the original author felt it necessary to second-guess the first clause. It seems to me, since this is structured as an OR, that if sql_affectedrows() returns 0 (for whatever reason), then the overall if statement would be true and the code it encloses (which is the INSERT statement, btw) would execute. That's my theory. |
|
|
UTC
quote
ScooterWoman9988 wrote: So you're saying the search results are held in a temp table of some sort? I thought they were INSERTed into a permanent table that has been holding all the search results since inception of the site. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
jess wrote: This might have something to do with it. This is the test to see if the update worked: if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() ) The first clause is the standard form used throughout this forum software: execute the SQL command and check the result. The second clause is much less common -- and I'm not sure why the original author felt it necessary to second-guess the first clause. It seems to me, since this is structured as an OR, that if sql_affectedrows() returns 0 (for whatever reason), then the overall if statement would be true and the code it encloses (which is the INSERT statement, btw) would execute. That's my theory. |
|
|
UTC
quote
ScooterWoman9988 wrote: What comes after the if? That codes looks to me like if SQL command fails (in this case INSERT) OR affectedrows is false THEN (do something). I'm wondering what the do something is. Throw an error? mt_srand ((double) microtime() * 1000000); I'm thinking that the first clause (the sql_query() that performs the actual UPDATE SQL statement) succeeds, but (for reasons still unknown) sql_affectedrows() returns 0, causing the INSERT to be executed with the same key.$search_id = mt_rand(); $sql = "UPDATE " . SEARCH_TABLE . " SET search_id = $search_id, search_time = $current_time, search_array = '" . str_replace("\'", "''", $result_array) . "' WHERE session_id = '" . $userdata['session_id'] . "'"; if ( !($result = $db->sql_query($sql)) || !$db->sql_affectedrows() ) { $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_time, search_array) VALUES($search_id, '" . $userdata['session_id'] . "', $current_time, '" . str_replace("\'", "''", $result_array) . "')"; if ( !($result = $db->sql_query($sql)) ) { message_die(GENERAL_ERROR, 'Could not insert search results', '', __LINE__, __FILE__, $sql); } } That message_die() function call is what produced the error message you saw. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
Agreed. Seems odd though that any database engine would return an erroneous result on affected rows.
|
|
|
UTC
quote
ScooterWoman9988 wrote: Seems odd though that any database engine would return an erroneous result on affected rows. |
|
OP
UTC
Hooked
Genuine Buddy 125, Piaggio BV 250
Joined: UTC
Posts: 146 Location: NE CT |
|
|
OP
|
UTC
quote
Well, it does seem like it's returning a wrong result, either from the update or the rows affected. Weird.
|
Modern Vespa is the premier site for modern Vespa and Piaggio scooters. Vespa GTS300, GTS250, GTV, GT200, LX150, LXS, ET4, ET2, MP3, Fuoco, Elettrica and more.
