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 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B