dbTalk Databases Forums  

[MySQL] Double INSERT?

comp.databases comp.databases


Discuss [MySQL] Double INSERT? in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jag
 
Posts: n/a

Default [MySQL] Double INSERT? - 09-27-2006 , 10:14 AM






Hello there.
I have the very strange problem with
the MySQL database [4.1.18]. I'm using
it with PHP5.
I have the table "pages_view" which
is counting pages shows. Structure
of the table looks like that:

pvID - BIGINT AUTO_INCREMENT
pvHash - VARCHAR(10)
pvVisits - BIGINT

pvID - index of the table
pvHash - Hash of main_menu and sub_menu
(for recognizing the page)
pvVisits - main counter

PHP Code looks like that:

$hasz=$this->CreateMenuHash();
$r=mysql_query("SELECT pvID, pvVisits FROM pages_view WHERE
pvHash='".$hasz."'";
$ile=mysql_count_rows($r);
if ($ile==1)
{
$d=mysql_fetch_assoc($r);
$odslon=$d["pvVisits"];
$odslon++;
$r=mysql_query("UPDATE pages_view SET pvVisits='".$odslon."' WHERE
pvID=".$d["pvID"];
echo "Viewed: ".$odslon;
} else {
$r=mysql_query("INSERT INTO pages_view (pvVisits, pvHash) VALUES
('1','".$hasz."')");
echo "Viewed: 1";
}

It works perfectly well for the first show.
It shows "Viewed: 1" and it's ok.

When I press "Refresh" - troubles begins.
It should show "Viewed: 2" but it doesn't.
I got "Viewd: 3". When I press "Refresh"
againa, I got "Viewed: 5". Nothing is pointing
at the fact that the code is being executed
twice.
It is strange, that double counting is showing
under IE and FireFox. Under Opera it works
perfectly well, counting one by one.
Maybe somebody can help?
Thanks in above.

PS: sorry for my english.

--
pozdrawiam
Jag



Reply With Quote
  #2  
Old   
Ronnie Chee
 
Posts: n/a

Default Re: [MySQL] Double INSERT? - 09-27-2006 , 11:57 AM






"Jag" <jag (AT) jca (DOT) no-spampls.iserwer.pl> wrote

Quote:
Hello there.
I have the very strange problem with
the MySQL database [4.1.18]. I'm using
it with PHP5.
I have the table "pages_view" which
is counting pages shows. Structure
of the table looks like that:

pvID - BIGINT AUTO_INCREMENT
pvHash - VARCHAR(10)
pvVisits - BIGINT

pvID - index of the table
pvHash - Hash of main_menu and sub_menu
(for recognizing the page)
pvVisits - main counter

PHP Code looks like that:

$hasz=$this->CreateMenuHash();
$r=mysql_query("SELECT pvID, pvVisits FROM pages_view WHERE
pvHash='".$hasz."'";
$ile=mysql_count_rows($r);
if ($ile==1)
{
$d=mysql_fetch_assoc($r);
$odslon=$d["pvVisits"];
$odslon++;
$r=mysql_query("UPDATE pages_view SET pvVisits='".$odslon."' WHERE
pvID=".$d["pvID"];
echo "Viewed: ".$odslon;
} else {
$r=mysql_query("INSERT INTO pages_view (pvVisits, pvHash) VALUES
('1','".$hasz."')");
echo "Viewed: 1";
}

It works perfectly well for the first show.
It shows "Viewed: 1" and it's ok.

When I press "Refresh" - troubles begins.
It should show "Viewed: 2" but it doesn't.
I got "Viewd: 3". When I press "Refresh"
againa, I got "Viewed: 5". Nothing is pointing
at the fact that the code is being executed
twice.
It is strange, that double counting is showing
under IE and FireFox. Under Opera it works
perfectly well, counting one by one.
Maybe somebody can help?
Thanks in above.

PS: sorry for my english.

Works as programmed.
You have defined "pvID" as AUTO_INCREMENT and then in your code you
increment it again with "$odslon++".




Reply With Quote
  #3  
Old   
Jag
 
Posts: n/a

Default Re: [MySQL] Double INSERT? - 09-27-2006 , 02:40 PM



User "Ronnie Chee" <cheer@[127.0.0.1]> wrote:
Quote:
Works as programmed.
You do not understand single line from
this code, is that right?

Quote:
You have defined "pvID" as AUTO_INCREMENT
and then in your code you increment it
again with "$odslon++".
pvID is AUTO_INCREMENT I agree.
But...

Quote:
$odslon=$d["pvVisits"];
Is that pvID?

Quote:
$odslon++;
$r=mysql_query("UPDATE pages_view SET pvVisits='".$odslon."' WHERE
Is that pvID?

I can't figure it out, but don't treat
me like a fool.

And besides, where is explanation (if this
what you wrote accept as truth) why it
behave in different ways under different
browsers.

--
best regards
Jag




Reply With Quote
  #4  
Old   
Ronnie Chee
 
Posts: n/a

Default Re: [MySQL] Double INSERT? - 09-27-2006 , 04:05 PM




"Jag" <jag (AT) jca (DOT) no-spampls.iserwer.pl> wrote

Quote:
User "Ronnie Chee" <cheer@[127.0.0.1]> wrote:
Works as programmed.

You do not understand single line from
this code, is that right?

You have defined "pvID" as AUTO_INCREMENT
and then in your code you increment it
again with "$odslon++".

pvID is AUTO_INCREMENT I agree.
But...

$odslon=$d["pvVisits"];

Is that pvID?

$odslon++;
$r=mysql_query("UPDATE pages_view SET pvVisits='".$odslon."' WHERE

Is that pvID?

I can't figure it out, but don't treat
me like a fool.

And besides, where is explanation (if this
what you wrote accept as truth) why it
behave in different ways under different
browsers.

My apologies. I read it quickly and posted a stupid and careless answer.
You are right and I was very wrong.

FWIW ...
Did you try verifying the value of the field via the Query Analyzer before
doing the refresh?
Or echoing the relevant values before and after the increment step and
before the update step?

Again my apologies.






Reply With Quote
  #5  
Old   
Jag
 
Posts: n/a

Default Re: [MySQL] Double INSERT? - 09-28-2006 , 12:55 AM



User "Ronnie Chee" <cheer@[127.0.0.1]> wrote:
Quote:
My apologies. I read it quickly and posted
a stupid and careless answer.
You are right and I was very wrong.
There is no matter, everything in order.
I am apologizing, I reacted also too much.

Quote:
Did you try verifying the value of the field
via the Query Analyzer before doing the refresh?
Not exactly via QA but I did it like this:

$hasz = $this->CreateMenuHash();

At the beginning of the page:

$r=mysql_query("SELECT pvVisits FROM pages_view WHERE pvHash='".$hasz."'");
$d=mysql_fetch_row($r);
echo $d[0]; // it is for example 10.

Now do my function somewhere in the middle:

Read 10
Add one
Write 11

At the end of the page:

$r=mysql_query("SELECT pvVisits FROM pages_view WHERE pvHash='".$hasz."'");
$d=mysql_fetch_row($r);
echo $d[0]; // it is 11 - and works!

BUT after page is loaded, I used phpMyAdmin
to make sure what is pvVisits...
and it is 12!

Quote:
Or echoing the relevant values before and after
the increment step and before the update step?
Of course.

In the polish group pl.comp.bazy-danych (pl.comp.databases)
somebody pointed me I can use some reload-script which
works under IE & FireFox but not under Opera. I find
it interesting, but I do not use any JavaScript, or
header("location: ..., or <meta http-equiv="Refresh"
content="0; URL=http://...

--
best regards
Jag




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.