Reputation: 325
Using AJAX I try to update table 'forminfo' in a MySQL database, calling the php script via GET and providing an id (integer value). However, when I call changeRequestedQ.php?id=4 for instance, the table cell will update to 0, not 4.
The column 'requestedQ' is formatted as INT(11). When I try to update $id = 4
manually, it works and the table cell is updated to 4.
require_once("msqli_config.php");
$id = $_GET['id']; // doesn't work, table cell updates to 0, whatever integer value I pass through file.php?id=<int>
$id = (int)$_GET['id']; // no change in outcome
$id = 4; // works, table cell updates to 4
try {
$stmt1 = $mysqli->prepare("UPDATE forminfo SET requestedQ = ? WHERE id = 1;");
$stmt1->bind_param("i", $id);
$stmt1->execute();
$stmt1->close();
} catch (Exception $e) {
error_log($e);
exit();
}
$mysqli->close();
I have tried forcing PHP to parse the value as integer using (int)$_GET['id']
, but there was no difference.
Additional variations (not using prepared statements):
$sql = "UPDATE forminfo SET requestedQ = '4' WHERE id = 1";
$mysqli->query($sql); // updates to 4
echo $sql; // UPDATE forminfo SET requestedQ = '4' WHERE id = 1
$sql = "UPDATE forminfo SET requestedQ = 4 WHERE id = 1";
$mysqli->query($sql); // updates to 4
echo $sql; // UPDATE forminfo SET requestedQ = 4 WHERE id = 1
$sql = "UPDATE forminfo SET requestedQ = '" . $id . "' WHERE id = 1";
$mysqli->query($sql); // updates to 0
echo $sql; // UPDATE forminfo SET requestedQ = '4' WHERE id = 1
$sql = "UPDATE forminfo SET requestedQ = " . $id . " WHERE id = 1";
$mysqli->query($sql); // updates to 0
echo $sql; // UPDATE forminfo SET requestedQ = 4 WHERE id = 1
UPDATE Being desperate, as I couldn't get my head around the difference in result between the previously mentioned pieces of code, I tried if this might be a specific browser issue. It turnes out that the above code works like a charm in MS Edge, and also in Google Chrome in Incognito mode, just not in a normal session. How can this be? Does it have to do with caching? I disabled cache in developers tools, but this didn't help...
Upvotes: 1
Views: 993
Reputation: 325
I've solved my problem. Thinking that this was a PHP/MySQL thing, I never looked into my browsers behaviour, nor the request and response parameters. My bad...
It turnes out that a browser plugin in Chrome was sending a second empty GET request (so without the query string) to the same destination, after the original request finished loading. Withoud $_GET['id']
defined, the table cell updates to 0. Removed the plugin, everything works.
Upvotes: 1