简体   繁体   English

没有子查询的“子查询返回多于1行”的MySQL?

[英]“Subquery returns more than 1 row” with no subquery MySQL?

I have a webapp that when a link for a document is clicked, a PHP script is called that updates the "viewed" status for the document in the DB. 我有一个webapp,当单击文档链接时,将调用PHP脚本,该脚本更新数据库中文档的“已查看”状态。 This is the script that is called when the click happens: 这是单击发生时调用的脚本:

<?php
include '../../variables.php';

// The document that is passed through POST
$document = $_POST['document'];

$conn = new mysqli($dbhost, $dbuser, $dbpassword, $db);

if (!$conn){
    die('Could not connect to db: '.mysqli_error($conn));
}

$sql = "UPDATE files
        SET docViewed = '1'
        WHERE fileloc = '$document'";
$query = mysqli_query($conn, $sql);

if (!$query){
    die('Could not update docViewed: '.mysqli_error($conn));
}
?>

As you can see, I have no subquery in the MySQL query that updates the field I want, yet I still am receiving this error: 如您所见,我在MySQL查询中没有子查询来更新所需的字段,但仍然收到此错误:

Could not update docViewed: Subquery returns more than 1 row 无法更新docViewed:子查询返回的行数超过1

I have tried appending to the query: 我试过追加到查询:

"...
WHERE fileloc = '$document'
LIMIT 1";

However, I still get the same result. 但是,我仍然得到相同的结果。

To be clear, each $document has to be UNIQUE in the database, so there are no duplicate entries. 需要明确的是,每个$document在数据库中必须是UNIQUE,所以没有重复的条目。

UPDATE: This post is not a duplicate of the suggested post, as that OP is using a subquery. 更新:此职位不是建议的职位的重复,因为该OP正在使用子查询。 In this example, I am not using a subquery anywhere. 在此示例中,我不在任何地方使用子查询。

Here is the structure of the files table I use. 这是我使用的files表的结构。 Also to show there is no duplicate of $document , I filtered the table by the fileloc which is 30294/1506012960606.pdf : 同样为了显示没有$document重复项,我通过fileloc过滤了表30294/1506012960606.pdf

在此处输入图片说明

UPDATE 2: I have narrowed down the actual MySQL query that is happening to produce this error: 更新2:我已经缩小了实际发生此错误的MySQL查询的范围:

UPDATE files
SET docViewed = '1'
WHERE fileloc = '30294/1492682311085.pdf'

This doesn't change too much, but adds some more logging points to maybe drill down on where the issue is. 这不会发生太大变化,但是会添加更多的日志记录点,以便深入研究问题所在。 I updated your mysqli usage to the object oriented approach, as well as parameterizing the queries using prepared statement (always good to avoid sql injection, but in this case extra practical because it lets us test the query in several steps). 我将您的mysqli用法更新为面向对象的方法,并使用准备好的语句对查询进行参数化(总是很好避免SQL注入,但是在这种情况下,由于它使我们可以在多个步骤中测试查询,因此更加实用)。

<?php
try {
    include '../../variables.php';

    // The document that is passed through POST
    $document = filter_input(INPUT_POST, 'document', FILTER_SANITIZE_STRING);

    $conn = new mysqli($dbhost, $dbuser, $dbpassword, $db);

    if ($conn->connect_error){
        throw new Exception("({$conn->errno}) {$conn->error}");
    }

    $sql = "UPDATE files
            SET docViewed = '1'
            WHERE fileloc = ?";
    $stmt = $conn->prepare($sql);

    if (!$stmt) {
        throw new Exception("({$conn->errno}) {$conn->error}");
    }
    $stmt->bind_param('s', $document);
    $exec = $stmt->execute();

    if (!$exec) {
        throw new Exception($stmt->error);
    } else if ($stmt->affected_rows === 0) {
        throw new Exception('No file location found');
    }


} catch (Exception $e) {
    error_log($e);
    die($e->getMessage());
}
?>

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

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