dtnpf35197 2019-01-26 17:57
浏览 184
已采纳

使用bind_param时,MySQL UPDATE将值更改为“0”而不是提供的整数

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...

  • 写回答

1条回答 默认 最新

  • dsa1230000 2019-01-27 12:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)