3

I can't figure out why this code is inserting the same row twice. I've literally stripped it back to the following code:

<?php

$query = "INSERT INTO `cs_social_alerts` (email) VALUES ('test@test.com')";
mysql_query($query);

?>

The MySQL table it's being inserted into has 10 columns in it, but even with all of them mentioned in the query, it still inserts 'test@test.com' on two rows, with separate primary keys.

I've created a new WordPress page to run this on as all other pages seem to be functioning fine without the duplication.

I've done some Googling which hasn't found much of any help - Is there any way I can check where the second query is coming from? I've starred at the above code for about an hour now and cannot see any issues with it.

So here's the result from the debug traceback, the code that's being run is literally the 2 lines above - I've blanked the domain for security. Can anyone see any interference?

#0 eval() called at [/var/sites/c/****.com/public_html/wp-content/plugins/wp-exec-php/wp-exec-php.php:652]
#1 WP_exec_PHP->exec(
$myQuery = "INSERT INTO `cs_social_alerts` (email) VALUES ('test@test.com')";

mysql_query($myQuery);

debug_print_backtrace()

?>

) called at [/var/sites/c/****.com/public_html/wp-content/plugins/wp-exec-php/wp-exec-php.php:692]
#2 WP_exec_PHP->_exec_post(
$myQuery = "INSERT INTO `cs_social_alerts` (email) VALUES ('test@test.com')";

mysql_query($myQuery);

debug_print_backtrace()

?>

)
#3 call_user_func_array(Array ([0] => WP_exec_PHP Object ([] => Array (),[] => /var/sites/c/*****.com/public_html/wp-content/plugins/wp-exec-php/wp-exec-php.php),[1] => _exec_post), Array ([0] =>
$myQuery = "INSERT INTO `cs_social_alerts` (email) VALUES ('test@test.com')";

mysql_query($myQuery);

debug_print_backtrace()

?>

)) called at [/var/sites/c/*****.com/public_html/wp-includes/plugin.php:192]
#4 apply_filters(the_content,
$myQuery = "INSERT INTO `cs_social_alerts` (email) VALUES ('test@test.com')";

mysql_query($myQuery);

debug_print_backtrace()

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ryan
  • 605
  • 1
  • 5
  • 12

4 Answers4

0

Use the PHP function debug_print_backtrace() to find out where your code is being called from.

Zebra North
  • 11,412
  • 7
  • 37
  • 49
  • Thanks for the debug script, but I still can't figure out why it's happening, because it's Wordpress, there's all sorts of call going on on the page :( – Ryan Aug 12 '14 at 20:06
  • That's the only way you're going to find out where it's being called from I'm afraid. Post the backtraces if you want further help. – Zebra North Aug 12 '14 at 20:29
0

Try a PHP Data Object(PDO). Using the mysql_* functions are obsolete.

These variable initializations should be defined in an ini file that you use php_parse_ini() to get the data from.

<?php        
    $host = "host=localhost";
    $name = ";dbname=name_of_db";
    $user = "user";
    $pass = "pass";

    $conn = new PDO("mysqli:".$host.$name,$user,$pass);
    $stmt = $conn->prepare("INSERT INTO `cs_social_alerts` (email) VALUES (:email)");
    $stmt->bindParam(':email', $email);
    $stmt->execute();

Also, if you want to know if this code is getting run more than once. Try setting a $_SESSION variable. I.E. $_SESSION['count'] = 0; Then right before execute() put $_SESSION['count']++; Finally, dump the value of $_SESSION at the end of your code to determine what the value is.

var_dump($_SESSION);die();
0

I had the same problem, and it was chrome fault! I clear the sessions, cookies, cache, all data application and it works.

chispitaos
  • 767
  • 9
  • 14
  • 1
    This had me for days! but your solution was my saviour. And I must say that Chrome of late has been needing more and more cache clearance. – warmwhisky Nov 25 '17 at 20:21
-1

Have you tried another browser? I had plenty of bad experiences because some extensions that I had in my browser were requesting the page one more time.

Even if that's not your problem, I think you should check for external factors, as this code cannot insert two rows. Unless, of course, it's being called twice.

Denis Lins
  • 816
  • 7
  • 22
  • I have the same issue even when I put an `exit;` or `die()` command immediately after in an attempt to ensure it only gets called once. – G-J Mar 01 '19 at 14:05