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

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

图片转代码服务由CSDN问答提供 功能建议

使用AJAX我尝试在MySQL数据库中更新表'forminfo',通过GET调用php脚本并提供 id(整数值)。 但是,当我调用changeRequestedQ.php?id = 4时,表格单元格将更新为0而不是4。</ p>

“requestedQ”列格式为INT(11)。 当我尝试手动更新 $ id = 4 </ code>时,它可以工作,表格单元格更新为4. </ p>

  require_once(“msqli_config.php”  ); 
 
 $ id = $ _GET ['id'];  //不起作用,表格单元格更新为0,无论我通过file.php传递的整数值?id =&lt; int&gt; 
 $ id =(int)$ _ GET ['id'];  //结果没有变化
 $ id = 4;  //有效,表格单元格更新为4 
 
try {
 $ stmt1 = $ mysqli-&gt; prepare(“UPDATE forminfo SET requestedQ =?WHERE id = 1;”); 
 $ stmt1-&gt; bind_param(  “i”,$ id); 
 $ stmt1-&gt; execute(); 
 $ stmt1-&gt; close(); 
} catch(异常$ e){
 error_log($ e); 
  exit(); 
} 
 
 $ mysqli-&gt; close(); 
 </ code> </ pre> 
 
 

我试图强制PHP使用<将值解析为整数 code>(int)$ _ GET ['id'] </ code>,但没有区别。</ p>

其他变体(不使用预准备语句):</ p> \ n

  $ sql =“UPDATE forminfo SET requestedQ ='4'WHERE id = 1”; 
 $ mysqli-&gt; query($ sql);  //更新到4 
echo $ sql;  // UPDATE forminfo SET requestedQ ='4'WHERE id = 1 
 
 $ sql =“UPDATE forminfo SET requestedQ = 4 WHERE id = 1”; 
 $ mysqli-&gt; query($ sql);  //更新到4 
echo $ sql;  // UPDATE forminfo SET requestedQ = 4 WHERE id = 1 
 
 $ sql =“UPDATE forminfo SET requestedQ ='”。  $ id。  “'WHERE id = 1”; 
 $ mysqli-&gt; query($ sql);  //更新到0 
echo $ sql;  // UPDATE forminfo SET requestedQ ='4'WHERE id = 1 
 
 $ sql =“UPDATE forminfo SET requestedQ =”。  $ id。  “WHERE id = 1”; 
 $ mysqli-&gt; query($ sql);  //更新到0 
echo $ sql;  // UPDATE forminfo SET requestedQ = 4 WHERE id = 1 
 </ code> </ pre> 
 
 

UPDATE </ strong> 绝望,因为我无法理解 前面提到的代码之间的结果差异,我试过,如果这可能是一个特定的浏览器问题。 事实证明,上述代码就像MS Edge中的魅力,以及隐身模式下的谷歌浏览器,只是不在正常会话中。 怎么会这样? 它与缓存有关吗? 我在开发人员工具中禁用了缓存,但这没有帮助...... </ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题