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...
$_GET?array(1) { ["id"]=> string(1) "4" }as expected$stmt1 = $mysqli->prepare("UPDATE forminfo SET requestedQ = " . $id . " WHERE id = 1;"); //$stmt1->bind_param("i", $id);just to rule out the binding as the issue.$idvariable. Is there code between the assignment, yourvar_dump, and the query execution?$idbeing reassigned to a string is the only thing I could see.