简体   繁体   English

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

[英]Query Returns Success But no Rows Updated

I'm currently having an issue where my query returns a success however there is no actual update that occurs when I check my SQL database. 我目前遇到查询返回成功的问题,但是当我检查SQL数据库时没有发生实际更新。 Oddly enough, when I copy the same exact query into phpMyAdmin, a response is successfully returned and the query works just fine, the rows are updated. 奇怪的是,当我将相同的确切查询复制到phpMyAdmin中时,成功返回了响应,并且查询工作正常,行已更新。 ( Note: I'm well aware of the high risk of SQL injection, however mysqli_escape_string isn't working for some reason so I'll worry about that when I go into the production stage. ) 注意:我很清楚SQL注入的高风险,但是由于某些原因mysqli_escape_string无法正常工作,因此在进入生产阶段时我会为此担心。

script.php script.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);

The variables $fave and $unfave would return values like so: 'abcd123','dcba321','hello123' , which would make the query look like so: 变量$fave$unfave将返回如下值: 'abcd123','dcba321','hello123' ,这将使查询看起来像这样:

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

Now, entering the query into phpMyAdmin works just fine, but when doing it through php, the response returns a success however no rows are actually updated, so I'm not sure what is going on as my php error.log is as clean as a whistle. 现在,将查询输入phpMyAdmin可以正常工作,但是通过php进行查询时,响应返回成功,但是实际上没有行被更新,因此我不确定发生了什么,因为我的php error.log是否干净吹口哨。

Also, if you're wondering what my require_once connection.php file looks like which connects me to the database, it is the following: 另外,如果您想知道将我连接到数据库的我的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在单引号中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM