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 (关键词-聊天软件)
    • ¥15 求大家看看这个编程的编法没有思路啊
    • ¥20 WSL打开图形化程序子窗口无法点击
    • ¥15 Jupyter Notebook 数学公式不渲染
    • ¥20 ERR_CACHE_MISS 确认重新提交表单
    • ¥20 关于vba使用HTMLfile执行js函数问题
    • ¥60 悬赏求解,通过实时现场摄像头的视频图像识别其他对家打出的麻将牌,识别麻将牌,识别牌墙位置,通过识别对家打出了什么牌
    • ¥15 关于#GPU jetson#的pcie驱动开发问题,如何解决?
    • ¥15 stm32f103zet6 串口5无法收发数据
    • ¥15 关于C语言使用线程队列实现多线程并发