I am trying to update one of my row with PHP using MySQLi and if I refresh PHPMyAdmin less than 1 second after update it’s sucessfull but if I only check the data 2 seconds+ after the update it doesn’t update.
For exemple:
'UPDATE orders SET MachineID = '.$id.' WHERE OrderID = '.$OrderID.
'AND ProductID = '.$ProductID
Does not work unless I refresh quickly but…
'UPDATE orders SET MachineID = 2 WHERE OrderID = 4 AND ProductID = 12'
Will work no matter how long after I refresh. (Those are the data I normaly use to test.)
So I though it would be my variables, but I’m using them almost 10 times before this part of the code in other queries and it works perfectly.
- I tried to trim() the variable and it did not help.
- I also tried to use mysqli_real_escape_string() with no success.
- mysqli_error() is not giving me anything.
- mysqli_affected_rows() is giving me “1” which is what it is suppose to be.
- And the weird part is if I execute ‘SELECT MachineID FROM orders WHERE OrderID = 4 AND ProductID = 12’, it gives me the updated answer even if phpMyAdmin does not update the data.
There is no other code after this, so nothing that could “reverse” the update.
A normal “test” ouput looks like this:
Edit: This it what the browser outputs.
ID: "2" //$id
OrderID: "4" //$OrderID
ProductID: "12" //$ProductID
UPDATE orders SET MachineID = 2 WHERE OrderID = 4 AND ProductID = 12 //Query
boolean true //Result var_dump
1 //Number of rows affected
2 //Machine ID
Note: The quotes are not part of the variables.
Edit: This is the PHP code
$query = 'UPDATE orders SET MachineID = '.$id.' WHERE OrderID = '.$OrderID.' AND ProductID = '.$ProductID;
echo 'ID: "'.$id.'"<br/>
OrderID: "'.$OrderID.'"<br/>
ProductID: "'.$ProductID.'"<br/>'.$query.'<br/>';
$res = $db->query($query);
var_dump($res);
echo mysqli_affected_rows ($db).'<br/>';
$result = $db->query('SELECT MachineID FROM orders WHERE OrderID = 4 AND ProductID = 12');
$result = $result->fetch_array();
echo $result[0];
I really don’t understand why it would work in the file and after a quick refresh but not if it takes to long to retreive the data. It’s like it would reset after a certain amount of time if it’s not fetch.
I’ve been working on this for almost 2 days now I have no idea why it’s not working. This is some pretty simple SQL query.
Edit: I’ve look into MySQL binary logs and it seems that I’m updating it back to 1 every time. The only way this could be happening is if the file would run twice, but if it runs twice why would the output be there only once ?
Edit: Ok, so it seems like the problem comes from Google Chrome. I’ve tested it on IE and it works. For some reason Chrome would be running the file twice.
Ok so I was able to find the problem and fix it.
Problem:
The problem was the famous “favicon” bug with Chrome which is trying to get the icon even if it doesn’t exist, therefore the file is called twice.
Fix:
Since this file was meant to be called through an Ajax call the bug is not triggered since Chrome will not try to look for the “favicon”. I basicly fixed the bug by testing it how it was supposed to be run and not just testing the file itself.