I must be doing really stupid here. I’ve been troubleshooting this for 3 days with no luck.
Here is the PHP string that is sent to mysql_query():
insert into post_replies (postID, fromID, toID, status) values ($id, $userID, $toID, ".g_unread.")
Before you say, “Ah-hah, there must be an error in the SQL”, please note these two things:
- mysql_query() returns true (1)
- I export the string to a text file (see below) BEFORE sending to mysql_query() – If I paste the exported text into the same mysql_query() statement, it works fine (new row appears) – If paste the exported text into phpMyAdmin, it works fine (new row appears)
- The exact (and I mean exact, as in I simply copy and pasted it) same query is used a few lines down and it always works fine
Another thing I tried was to load the query back from the text and then send it to mysql_query() – but that produces the same odd behavior – it returns success (1), yet no new rows are actually inserted.
I have showed this to another programmer, who complained about my PHP coding style. He said INTs should be wrapped as strings and I should use curly braces around those (e.g. ‘{$id}’). However, of course that fixed nothing, the behavior was exactly the same. If there was a problem with string generation then the problem would appear in the query that is exported to text file before sending to mysql_query(). He also complained that I was using outdated mysql functions (that I should be using mysqli functions). However, that does not explain why my hundreds of other queries sent via mysql_query() work fine, including the one a few line below this one. It does not explain why the query works fine if I paste the exported-to-text-file text directly into mysql_query().
Thank you so much. I am totally lost here.
UPDATE #1: By request, here is the code around the statement:
$q = "insert into post_replies (postID, fromID, toID, status) values ($id, $userID, $toID, ".g_unread.")";
$result = mysql_query($q);
if ($result == true) {
z($q);
} else {
z('failure');
}
z() is a function I use for easily exporting to text file. Btw, the query is always sent to z(), ‘failure’ is never sent.
Here is an example of what is sent to the text file:
insert into post_replies (postID, fromID, toID, status) values (2039, 8, 1, 1)
Please keep mind that if I paste that text into phpMyAdmin, or into the mysql_query() function above, the row is inserted.
UPDATE #2: Due to concern by some, I have updated this part of the code to use mysql[i]
$mysqli = new mysqli(g_db_server, g_db_user, g_db_pass, g_db);
$result = $mysqli->query($q);
I now make a separate connection, just for this single query. However, the behavior is exactly the same. It returns true, but no news rows are created. However, if I export $q to a text file, and then paste that text into $mysqli->query() above, it works fine and the new row is created as expected.
Since everyone seems to have given up, I am now working on isolating the problem to single MySQL table / PHP file which anyone can download and try for themselves.
UPDATE #3: Solved! Sort of. It seems to be an issue with the surrounding code, causing the row to be deleted after insertion. However, this still doesn’t explain why mysql_insert_id() returned 0. Perhaps mysql_insert_id() breaks on composite keys. Anyhow, I don’t care – because it’s fixed. I will give the check to @user1231958 because his answer was the closest. Thank you all immensely for your help and sorry for being tricked by mysql_insert_id()
Try to use grave accent’s when specifying your tables and rows,
Tell me if if that works.