Reputation: 11
I have a strange problem. I have a table consisting of 4 columns.
Table Name is "Request" It is InnoDB. ReqSTATIC is the Primary Key.
ReqSTATIC mediumint(8) UNSIGNED auto_increment
RidSTATIC mediumint(8) UNSIGNED No
UserName varchar(15) utf8_general_ci No
RequestArea varchar(256) utf8_general_ci No
When I execute the following statements within a PHP file, it is generating 3 rows instead of 1 row. What could be causing this???? The program is NOT looping.
$InsertReqQuery="insert into request(RidSTATIC, UserName, Request) Values('$RidSTATIC', '$UserName','$Request')";
$InsertReqResult=mysql_query($InsertReqQuery);
if($InsertReqResult) {
echo "Insert is good";
}
Before the insert the Table was empty. After the insert phpMySQL shows the following:
ReqSTATIC RidSTATIC UserName RequestArea
1 5 jpizzolato test from phptest3
2 5 jpizzolato test from phptest3
3 5 jpizzolato test from phptest3
and to answer everyone's questions
Thank you everyone for taking a look at this. It appears that this is not a php and mysql issue. What follows below after the "**" is the entire code. It is in a Wordpress Page. The Page doesn't have anything else in it except what you see. The bazaar thing about this is the Wordpress website in question is hosted by GoDaddy. I duplicated this same exact test and same exact table in a HostGator hosted Wordpress website. The HostGator test was fine. It only inserted 1 row, as it should. The GoDaddy test is putting out 3 rows. To me there has to be something different in how Wordpress is defined. I know this isn't a Wordpress forum, but I didn't think this was a Wordpress issue, but maybe it is.
I called GoDaddy support but they weren't any help.
Yes, I only see "Insert is good" and "outside of if" displayed ONE time.
The only thing the databaseconfig.php file is doing is defining the server, database, username and password and performs a mysql_connect statement, nothing else
<?php
require_once("scripts/databaseconfig.php");//data base details included here
$RidSTATIC="5";
$UserName="jpizzolato";
$RequestArea="new test from phptest3";
$InsertReqQuery="insert into request(RidSTATIC,UserName,RequestArea)Values('$RidSTATIC','$UserName','$RequestArea')";
$InsertReqResult=mysql_query($InsertReqQuery);
if($InsertReqResult)
{
?>
<p><font color="#FFFFFF">insert is good</font></p>
<?php
}
?>
<p> outside of if </p>
?>
Upvotes: 1
Views: 1402
Reputation: 1
I just this week had the exact same situation of the insert occurring 3 times. With the clue above that it might be the browser repeatedly executing the script I indeed found that to be the case. The browser did this very quickly and each time it overlays EXACTLY what was on the screen from the prior execution. The only exception would be a time-stamp which I happened to have recorded in my SQL table which on occasion would be 1 sec later.
I was wanting to do a quick and dirty check out of a php example I had seen here (unrelated to this problem) and so had created a php file type with that code in it with a few teaks for my need mainly some echo'd output to the browser. I then thought since I was going to invoke this php file directly that I should place some in-stream html in the front (and end) to define the page. Well I had been too quick and did NOT include the 'html' tags. As soon as I put those in the problem was solved!
Upvotes: 0
Reputation: 86
I am having the same thing occur, only when posting my $wpdb->insert inside of a post. Moving the statement to the head resolves the issue. My guess is that the content is processed three times before being displayed. This could be due to plugins formating the code? I know I didn't have this issue until recently. So, it could be something like Jetpack that is doing it.
Upvotes: 1
Reputation: 461
If you call this file directly by URL, then check your web server access log files to make sure that your_file.php is called only once (no looping redirects and so on)
If this file is called as include from other files then use require_once instead of require or include for embedding it.
Upvotes: 2
Reputation: 24885
Triggers?
Other than that, change your code to modify one of the values (v.g., use a constant "Hello World")
$InsertReqQuery="insert into request(RidSTATIC, UserName, Request) Values '$RidSTATIC', '$UserName','Hello World!')";
to check if all resulting rows are the same (maybe you are reusing these variables in another part of your code?
Upvotes: 1