Insert Query on Page Load, Inserts Twice

SQL Insert query is executed twice

I can't see anything in your code that would execute it twice. I'd assume that it is being called twice. Put a break point at addNewComment and if it is being called twice look at the stack traces to see where it is being called from on both occasions.

Maybe you have an event being called twice for example. This can happen in ASP.NET if you both have auto wiring of events enabled and have wired the event up explicitly.

By the way you should definitely use parametrized queries not string concatenation. I'm assuming that comment is user supplied input? In which case you are setting yourself up for a SQL injection attack with the code you have shown.

PDO Inserts data twice on single query

Thanks to @James Taylor for the suggestion of .htaccess file. The problem was hiding in RewriteRule and parameter QSA.

PDO inserting the same data twice

->query executes it one time, ->execute does it a second time. You need to remove the execute.

Row inserts twice with one load

May be not an answer to your question, but I think you need to optimize your statements. Otherwise you can have like thousands of records with repeating ips and 0 or 1 times visited.

  • IP address - varchar -(UNIQUE KEY)
  • Times visited. - mediumint -

Then you could make it easier (UPDATED):

$ip = $mysqli->real_escape_string($_SERVER['REMOTE_ADDR']) ;
$update = "INSERT INTO visits (ip, times_visited) VALUES ('{$ip}', 1)
ON DUPLICATE KEY UPDATE times_visited = times_visited+1 ; " ;


Related Topics



Leave a reply



Submit