gpt4 book ai didi

php - 查询返回成功但没有更新行

转载 作者:行者123 更新时间:2023-11-29 21:47:06 24 4
gpt4 key购买 nike

我目前遇到的问题是,我的查询返回成功,但在检查 SQL 数据库时没有发生实际更新。奇怪的是,当我将相同的查询复制到 phpMyAdmin 时,成功返回响应,并且查询工作正常,行被更新。 (注意:我很清楚 SQL 注入(inject)的高风险,但是 mysqli_escape_string 由于某种原因无法工作,所以当我进入生产阶段时我会担心这一点。 )

脚本.php

$fave = json_decode($_POST['af']);
$unfave = json_decode($_POST['uf']);
$fave = "'".implode("','", $fave)."'";
$unfave = "'".implode("','", $unfave)."'";
if ($fave !== "''"){
$fq = "UPDATE post SET fave='1' WHERE 'an_id' IN ($fave) AND bid='$bizusr' AND fave='0'";
$r_fq = mysqli_query($GLOBALS["___mysqli_ston"], $fq);
$ar_fq = mysqli_affected_rows($GLOBALS["___mysqli_ston"]);
} else {
$r_fq = 1;
$ar_fq = 0;
}
if ($unfave !== "''"){
$ufq = "UPDATE post SET fave='0' WHERE 'an_id' IN ($unfave) AND bid='$bizusr' AND fave='1'";
$r_ufq = mysqli_query($GLOBALS["___mysqli_ston"], $ufq);
$ar_ufq = mysqli_affected_rows($GLOBALS["___mysqli_ston"]);
} else {
$r_ufq = 1;
$ar_ufq = 0;
}
if ($r_fq && $r_ufq){
$output = json_encode(array('type'=>'error', 'text' => "Favourites have been updated successfully. You've added $ar_fq favorites and removed $ar_ufq favorites." ));
die($output);
}
if (!$r_fq && $r_ufq){
$output = json_encode(array('type'=>'error', 'text' => "We've successfully favorited $ar_fq links, however there was an issue in unfavoriting some links, try refreshing." ));
die($output);
}
if ($r_fq && !$r_ufq){
$output = json_encode(array('type'=>'error', 'text' => "We've successfully unfavorited $ar_ufq links, however there was an issue in favoriting some links, try refreshing." ));
die($output);
}
if (!$r_fq && !$r_ufq){
$output = json_encode(array('type'=>'error', 'text' => "There was an error in updating your favorited links." ));
die($output);
}
// $un = mysqli_prepare($GLOBALS["___mysqli_ston"], "UPDATE analytics SET fave='0' WHERE an_id IN (?) AND bid= ? AND fave='1'");
// $fa = mysqli_prepare($GLOBALS["___mysqli_ston"], "UPDATE analytics SET fave='1' WHERE an_id IN (?) AND bid= ? AND fave='0'");
// mysqli_stmt_bind_param($un, 'ss', $unfave, $blockject);
// $a = mysqli_stmt_execute($un);
// mysqli_stmt_close($un);
// mysqli_stmt_bind_param($fa, 'ss', $fave, $blockject);
// $b = mysqli_stmt_execute($fa);
// mysqli_stmt_close($fa);

变量 $fave$unfave 将返回如下值:'abcd123','dcba321','hello123',其中将使查询看起来像这样:

UPDATE post SET fave='0' WHERE 'an_id' IN ('abcd123','dcba321','hello123') AND bid='$bizusr' AND fave='1';

现在,在 phpMyAdmin 中输入查询效果很好,但是当通过 php 执行此操作时,响应返回成功,但实际上没有更新任何行,所以我不确定发生了什么因为我的 php error.log 非常干净。

此外,如果您想知道将我连接到数据库的 require_once connection.php 文件是什么样的,它如下:

$link = ($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST,  DB_USER,  DB_PASSWORD));
if(!$link) {
die('Failed to connect to server: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
}

//Select database
$db = ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_DATABASE')));
if(!$db) {
die("Unable to select database");
}

最佳答案

愚蠢的我,我不知道为什么它返回成功的查询,但是将列 id an_id 用单引号引起来是问题

关于php - 查询返回成功但没有更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34026019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com