简体   繁体   English

INSERT INTO调用一次,但运行两次(不在循环中)

[英]INSERT INTO called once but runs twice (not in a loop)

I have a query which each time it is encountered, is run twice, which is not intended. 我有一个查询,每次遇到它都会运行两次,这不是故意的。 I have added logging to a file before and after it and clear the file before running the script. 我已经在文件前后添加了日志记录,并在运行脚本之前清除了文件。

I don't think I need to post the other code because I believe that my logging proves that this query is called only once. 我认为不需要发布其他代码,因为我认为我的日志记录证明此查询仅被调用一次。

The code: 编码:

file_put_contents(
    $_SERVER['DOCUMENT_ROOT'].'/PDOErrors.txt',
    "\n\nImmediately before ban query 1. ".date('r ')." (1)",
    FILE_APPEND);

$sth=$Mdbh->query("
    INSERT INTO
        banned_IP
    SET
        ip_add      = '$ip',
        proxy_ip    = '$proxy_ip'
");
$sth->execute();

file_put_contents(
    $_SERVER['DOCUMENT_ROOT'].'/PDOErrors.txt',
    "\n\nBanned the user using query1. ".date('r ')." (2)",
    FILE_APPEND);

return 999;

Note that there is a return immediately after it is run too and even if it was called twice, my PDOErrors.txt file would show the diagnostic data for each run, which it doesn't. 请注意,它也将在运行后立即return ,即使调用了两次,我的PDOErrors.txt文件也将显示每次运行的诊断数据,但不会显示。

The first diagnostic data into PDOErrors.txt runs but the second doesn't run ever! 进入PDOErrors.txt的第一个诊断数据将运行,但第二个则永远不会运行!

Here is a copy of the PDOErrors.txt 这是PDOErrors.txt的副本

Immediately before ban query 1. Thu, 21 Mar 2013 13:38:56 +0800  (1)

exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '127.0.0.1' for key 'ip_add'' in /home/peter/Documents/websites/Our_websites/bookkeeper/books.bookkeeper/public/includes/classes/loginAttempt.class.php:137
Stack trace:
#0 /home/peter/Documents/websites/Our_websites/bookkeeper/books.bookkeeper.ph/public/includes/classes/loginAttempt.class.php(137): PDOStatement->execute()
#1 /home/peter/Documents/websites/Our_websites/bookkeeper/books.bookkeeper.ph/public/includes/classes/login.class.php(980): loginAttempt->recordLoginAttempt(Array)
#2 /home/peter/Documents/websites/Our_websites/bookkeeper/books.bookkeeper.ph/public/ajax/login.user.php(25): Login->doLogin(Array)
#3 {main}

I should mention that I completely empty the banned_IP database table before each run. 我应该提到的是,在每次运行之前,我都完全清空了banned_IP数据库表。

I'm also logging all queries as this is a development environment and the log of the query can be seen below: 我也记录了所有查询,因为这是一个开发环境,查询日志可以在下面看到:

708 Query     INSERT INTO
                        banned_IP
              SET
                        ip_add          = '127.0.0.1',
                        proxy_ip        = ''

708 Query     INSERT INTO
                        banned_IP
              SET
                        ip_add          = '127.0.0.1',
                        proxy_ip        = ''
708 Quit

I think I'm satisfied that the query is only being called once as the PDOErrors.txt would include two sets of diagnostic data if not. 我想我对查询只被调用一次感到满意,因为PDOErrors.txt将包括两组诊断数据(如果没有)。 The error message appears to be generated on the very first run of the query on an empty database table. 该错误消息似乎是在对空数据库表的查询的第一次运行时生成的。 What could be causing this please? 请问是什么原因造成的?

PDO->query will execute a query and return PDOStatement object, now you are again executing the returned PDOStatement which cause this error. PDO-> query将执行查询并返回PDOStatement对象,现在您再次执行返回的PDOStatement,这将导致此错误。

Use PDO->prepare which returns PDOStatement object and then execute the statement OR just use PDO->query and DO NOT execute the PDOStatement 使用PDO-> prepare返回PDOStatement对象,然后执行该语句,或者只使用PDO-> query而不执行PDOStatement

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

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