简体   繁体   中英

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.

The first diagnostic data into PDOErrors.txt runs but the second doesn't run ever!

Here is a copy of the 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.

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

Use PDO->prepare which returns PDOStatement object and then execute the statement OR just use PDO->query and DO NOT execute the PDOStatement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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