0

I have a problem, I'm using pdo for make queries in my mysql database. When I want make an insert in my table of only one record, this insert TWO rows..

I dont know why insert two when this should insert only one. This is the php code:

<?php
$db = new PDO("mysql:host=localhost;dbname=mydatabase", "root", "");
$sql = "INSERT INTO test(lala)VALUES('xx1')";
$db->query($sql);

?>
JuanAndres
  • 27
  • 7

1 Answers1

2

Check HTTP requests.

Most likely your code acts as a 404 handler and there is an extra request from browser.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Hi,Is true !. The problem was the Mozilla Firefox Browser. I ran the same script in Chrome and this running correctly. But why happens these in Mozilla Firefox? – JuanAndres May 27 '14 at 14:34
  • The problem with your code that runs a query for the unknown request. – Your Common Sense May 27 '14 at 15:14
  • @YourCommonSense For some reason my PHP endpoint is being called twice, despite only sending one `fetch` from JavaScript and receiving only one response from the server. To solve this I checked to see if my `_POST` was null, and if it is then `die()`, which solved the problem. I don't really understand why this is happening, but maybe this is a characteristic of the browser, perhaps a pre-flight request, in which case it cannot be avoided and so we just need to code defensively? –  Jun 15 '20 at 12:58
  • 1
    @Pixel this is how sites are *intended* to work. there are thousands unrelated requests, be it a browser, or a lame bot, or a script-kiddie. This is why your endpoint should process only requests known to it and discard everything else. Just what you did. If you're curious, you can see in the developers console what particular request it is but it doesn't really matter. – Your Common Sense Jun 15 '20 at 13:04