What on earth is going on here? In the following code, the database is
inserted TWICE with the same values. Removing the "img" line removes
the problem.
------------------------------
$dbh = mysql_connect("localhost","username","password");
mysql_select_db("mydatabase",$dbh);
mysql_query("INSERT INTO test (name,title) VALUES ('12','34');",$dbh);
echo "<img src>"; // This line causes the entry to be inserted TWICE
------------------------------ |
Request for Question Clarification by
mother911-ga
on
12 Jul 2004 02:01 PDT
Hey Pete,
I figured since i'm not truly a mysql guy, but I noticed there seems
to be an odd closing section which would then pull in the echo line to
the insert line.
mysql_query("INSERT INTO test (name,title) VALUES ('12','34');",$dbh);
I think that should be:
mysql_query("INSERT INTO test (name,title) VALUES ('12','34')";,$dbh);
If this was just a typo, let me know. If I found our bug, let me know
so I can Answer the question and close out this dilemma.
|
Clarification of Question by
petetastic-ga
on
12 Jul 2004 10:43 PDT
Hi there,
Actually, the semi-colon has no effect, but if anything it should be
inside the quotes. Having it outside the quotes like you suggested
would terminate the php line of code and essentially split it into two
lines like this:
mysql_query("INSERT INTO test (name,title) VALUES ('12','34')";
,$dbh);
... giving a parse error.
|
Request for Question Clarification by
tox-ga
on
12 Jul 2004 11:40 PDT
petetastic-ga,
Perhaps something else in the rest of your code is causing this. Could
you post all the code from the php page that this is occuring?
Cheers,
tox-ga
|
Request for Question Clarification by
webadept-ga
on
12 Jul 2004 12:59 PDT
Hi,
Got a note from Mother911-ga to take a look at this .. I'm baffled.
The semi-colon isn't an issue, so we can stop looking at that. But
i've ran your code in a loop for 100 tests and didn't come up with a
single duplicate.. so i'm guessing that there is something else,
somewhere else that is doing this. A browser refresh perhaps? That's a
stretch..
Two idesa have come to mind however(and try these two in steps, not
all at once, that way you might be able to catch what is goin on
here.).
first try using persistant connection instead
http://us3.php.net/manual/en/function.mysql-pconnect.php
$dbh = mysql_pconnect("localhost","username","password");
mysql_select_db("mydatabase",$dbh);
mysql_query("INSERT INTO test (name,title) VALUES ('12','34');",$dbh);
echo "<img src>";
Next try closing the connection after the query
http://us3.php.net/manual/en/function.mysql-close.php
(normally this isnt' nessessary on a non-persistant connection, but
let's see what happens.. notice that I'm not using pconnect on this
one)
$dbh = mysql_connect("localhost","username","password");
mysql_select_db("mydatabase",$dbh);
mysql_query("INSERT INTO test (name,title) VALUES ('12','34');",$dbh);
mysql_close($dbh);
echo "<img src>";
With this last, a call to close the connection, it shouldn't matter
what comes after your query, nothing should cause it to duplicate the
insert. If a duplication is still happening, then you are some how
calling this area of your code again..all the way back up to making
the connection again and running the query, so you can stop looking at
the image tag and the echo (niether of which should have anyting to do
with causing a dup, but .. I've seen stranger things ).
Let us know what happens and we'll take it from there.
webadept-ga
|
Request for Question Clarification by
webadept-ga
on
12 Jul 2004 13:15 PDT
Hi again..
I got to thinking, talking this over with a fellow of mine, and
thought about that image tag you have there.
Now, don't take this hard, because I don't mean to be rough on you,
just trying to solve the problem... but normally you don't put things
like insert queries inside the body of an HTML document. I know you
can, but it's not really good form. So, it didn't cross my mind that
you might be doing that... what made it cross my mind is the echoing
the image tag.. which you could be doing in PHP code I suppose .. now
that I think about it.. but .. never mind all that..
What crosses my mind here is the <img src> tag itself. Some browsers,
IE5 for example, will refresh if the size of the image given in that
<img> tag is not the size of the image in the file that the src- is
pointing to. In order to re-size the image, the browser does a fast
refresh, sometimes you don't even see it, but its there.. this is the
same thing that causes some pages to load very slow, because the
browser refreshes several times before showing the page.
Now, if your code is caught in that browser refresh, then that is what
is doing this. Try chaning the size of the image to be the exact size
of the image src values, and see what happens. Chances are, that's
what's happening to you. There are several ways to fix this, other
than resizing all of your images, but lets' see if that is the
problem first.
webadpet-ga
|
Clarification of Question by
petetastic-ga
on
12 Jul 2004 14:43 PDT
Hi there,
Firstly, you're a rock star. Truly. Now, for the explanation:
I tried using mysql_pconnect, and mysql_close, separately and
together, with no joy.
I don't perform queries inside HTML documents - this error first
occurred inside a class file, a structure that was object-oriented,
abstracted, all of that good stuff. But after much gnashing of teeth,
I deleted every construct and line that wasn't necessary until I had
pared it down to these four lines. So, this represents the *entire*
code, but not necessarily my programming style :)
Now for the good news. I determined that this problem occurs in
Firefox (0.9) but NOT in IE (6). So I was thinking that perhaps it
does have something to do with what you said about browser refreshes.
True enough, the problem *only* occurs when there is NO src specified.
Somehow Firefox must do a refresh. I then went back to my code and
discovered that I was passing in an undefined variable as the image
name. Stupid, I know. But who would have thought this would lead to a
duplicate insert?!?
So, problem solved. Thanks a million. Now. er... how do I pay you?
Pete
|
Hi Pete,
Glad that worked out for you. I normally shy away from these
"diagnostic" type questions when there are so many variables possibly
going on. But since I seem to have stumbled onto the answer for you,
I'll gladly take the credit for it :-)
Something you might try as well, to see if it catches the error, just
in case the image file goes missing or someone desides it would look
better as a larger picture resized down by the browser (never know
what a user or .. gasp .. designer might do some day to your code) is
to keep the PHP from sending to the browser until all has been done,
then, the browser should only be refreshing the cache of the Server,
rather than directly at the code. Now, I'm not positive it will work
this way on every server, but at least you will have some type of
safty net ... since this isn't your code and you can't do much else
about it.
Anyway, to stop the direct feed to the browser you use ob_start, and ob_flush.
http://us4.php.net/manual/en/function.ob-start.php
Put ob_start at the top of your code, now the server starts caching
and nothing gets set to the browser until you call ob_flush, which
should be the last line of your code, and that sends, .. now.. if the
browser does its happy dance, it should only get the Server cache, and
not your code. .. maybe.. we hope.
Personally I would use sessions and cache in the last input id from
this user, and check to see if that id exists before running the sql
again. With the id and a micro time stamp, that will catch it for sure
and you won't have to worry about much at all. Yes, it is a bit of
code and some extra if statments there, but, with something as
volitile as image files on a web browser possibly making my data base
a fiction statement, I would do the extra code. Your milage may vary.
Have a great day,
webadept-ga |