I have an INSERT
query in my PHP
page which executes once as it is supposed to, and works fine, just how I want.
if(!$con->query("INSERT INTO contracts (CID, DATE, NUMBER, TITLE, DESCRIPTION) VALUES ('$cid', '" . $con->real_escape_string($date) . "', '" . $con->real_escape_string($number[$counter]) . "', '" . $con->real_escape_string($title[$counter]) . "', '" . $con->real_escape_string($description[$counter]) . "')"))
{
printf("Error message: %s
", $con->error);
}
Now, I add the following CSS
in the same PHP
file to make a table I output a bit nicer to read:
<style>
tr:nth-child(even) {
background-color: e5e4e2;
}
</style>
That makes every other row a grey colour, great... but wait! Now my SQL
INSERT
happens twice. I have no idea how these are connected, when I remove the CSS
, the INSERT executes once like I need.
Is there some jiggery pokery I am not understanding here? How can CSS
possibly affect my SQL
query?
For the record I am using XAMPP, PHP version 5.5.15 and phpMyAdmin for the database tables.
EDIT: I have checked the database, it is inserting twice.
The query is set to a button and I use if (isset($_GET['insertIt'])) {// INSERT QUERY
so that when the user clicks this button:<a href = 'testing.php?insertIt=true'><button style="height: 30px; margin-left: 10px; margin-right: 10px">2. Insert New Contracts To 'contracts' Table</button></a>
The query is sent. It works fine without the CSS
, but with, it runs twice
EDIT 2: OK, here are the two screenshots from Developer tools when I click the button. I found that it's not the omission of the CSS
exactly but the <style> ... </style>
tags; when I leave those out I get this:
When they are in my PHP
file I get this:
I can accept this and try to get around it by separating the files or leaving the tags out completely but I would still love to know why it happens. I am no expert but I can see there is an extra call to insertIt
when there are no <style>
tags and the entries are inserted twice.
For the record I want to be able to
- click the button
- insert the entries once
- and refresh the page as the page also displays the results of the insert.