By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,759 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,759 IT Pros & Developers. It's quick & easy.

INSERT into mysql alway make two entries

P: n/a
Got a simple problem.
I code some site and because Im a freak I made my own session-handling.
When a user open up my site it will check if there is a ssid in the url
if not generate one. this will be done by a function generate_ssid() and works fine.
next step is to register the ssid in the table session
this will be done by the following code:
************************************************** ***********************
function ssid_register($ssid,$login,$uid,$admin){

$connect = mysql_connect( $GLOBALS["db_host"],
$GLOBALS["db_user"],
$GLOBALS["db_password"])
or die($GLOBALS["db_error"]);
mysql_select_db($GLOBALS["db_name"],$connect) or die($GLOBALS["db_error"].'<br>ErrorCode s.001-con');
$result = mysql_query("INSERT INTO session SET ssid='$season',ip='$rip'",$connect);
echo ($rs);
return;
}
************************************************** **
I redesigned the INSERT INTO part into all possible syntax but everytime I look into the db
there are two entries with two DIFFERENT ssids.
The only way it works correct is to dont use a variable on the first place.
Means if I use ssid='godhelpmetofindaworkingsolution' it works great but is not realy what I want.

As told above a "INSERT INTO session (ssid) VALUES ($ssid)" also has the effect of two new db-entries.

Hope you understand and pray to god you know what I do wrong

Thx 4 any help
Sep 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Tue, 13 Sep 2005 23:32:40 +0200, "Johannes A. Brunner"
<jo**************@t-link.de> wrote:
Got a simple problem.
I code some site and because Im a freak I made my own session-handling.
When a user open up my site it will check if there is a ssid in the url
if not generate one. this will be done by a function generate_ssid() and works fine.
next step is to register the ssid in the table session
this will be done by the following code:
************************************************* ************************
function ssid_register($ssid,$login,$uid,$admin){

$connect = mysql_connect( $GLOBALS["db_host"],
$GLOBALS["db_user"],
$GLOBALS["db_password"])
or die($GLOBALS["db_error"]);
mysql_select_db($GLOBALS["db_name"],$connect) or die($GLOBALS["db_error"].'<br>ErrorCode s.001-con');
$result = mysql_query("INSERT INTO session SET ssid='$season',ip='$rip'",$connect);
echo ($rs);
return;
}
************************************************* ***
I redesigned the INSERT INTO part into all possible syntax but everytime I look into the db
there are two entries with two DIFFERENT ssids.
The only way it works correct is to dont use a variable on the first place.
Means if I use ssid='godhelpmetofindaworkingsolution' it works great but is not realy what I want.

As told above a "INSERT INTO session (ssid) VALUES ($ssid)" also has the effect of two new db-entries.


The INSERT can't be doing that. The most common mistake in this sort of thing
is setting up a race condition, so two requests both start a new session and
overlap saving to the database.

How are you detecting there's no ssid?

What are you doing with the ssid afterwards?

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 13 '05 #2

P: n/a
Andy Hassall wrote:
The INSERT can't be doing that. The most common mistake in this sort of
thing
is setting up a race condition, so two requests both start a new session
and overlap saving to the database.
But if I have a racecondition it would make no differenc using a variable or a fixed string.
$result = mysql_query("INSERT INTO session SET ssid='$season',ip='$rip'",$connect);
$result = mysql_query("INSERT INTO session SET ssid='season',ip='$rip'",$connect);

But the second line works fine. Just one db-entrie per page hit.
How are you detecting there's no ssid?
@$ssid = $_GET['ssid'];
if (!isset($ssid)) { $ssid = ssid_generate(); ssid_register($ssid,'false','unknown','false'); }

What are you doing with the ssid afterwards?


A lot of stuf,
Here is the way I create the ssid

function ssid_generate() {

$ssid = md5(time());
return $ssid;
}

Sep 14 '05 #3

P: n/a
$result = mysql_query("INSERT INTO session SET ssid='$season',ip='$rip'",$connect);


Andy's explanation is certainly correct: the reason you only get one
record if the ssid is hard-coded is probably because you have a unique
key constraint in the table so the second INSERT overwrites the first.
This doesn't happen if you generate the ssid because it is unique each
time. You must be calling the register function twice.

---
Steve

Sep 14 '05 #4

P: n/a
In article <b3***************************@nf1.news-service.com>,
"Johannes A. Brunner" <jo**************@t-link.de> wrote:
Got a simple problem.
I code some site and because Im a freak I made my own session-handling.
Why?
When a user open up my site it will check if there is a ssid in the url
if not generate one. this will be done by a function generate_ssid() and
works fine.
next step is to register the ssid in the table session
this will be done by the following code:
************************************************** ***********************
function ssid_register($ssid,$login,$uid,$admin){

$connect = mysql_connect( $GLOBALS["db_host"],
$GLOBALS["db_user"],
$GLOBALS["db_password"])
or die($GLOBALS["db_error"]);
I recommend doing a persistent connection to MySQL outside this function. This
way, you're making one new MySQL connection for every time the function is run.
If this is the only function that ever use MySQL, it's no big deal, but if it
isn't.

Lookup mysql_pconnect
mysql_select_db($GLOBALS["db_name"],$connect) or
die($GLOBALS["db_error"].'<br>ErrorCode s.001-con');
You actually don't need to select a database if you know it exists. You can use
"insert into database.sessions" below to insert into the correct database/table
$result = mysql_query("INSERT INTO session SET
ssid='$season',ip='$rip'",$connect);
echo ($rs);
return;
}
************************************************** **
I redesigned the INSERT INTO part into all possible syntax but everytime I
look into the db
there are two entries with two DIFFERENT ssids.
Why are you using insert into...set?

insert into database.sessions (ssid, ip) values ('$season', '$rip')

($season and $rip isn't even declared in the above function, so I'm guessing
you snipped some of the function.
The only way it works correct is to dont use a variable on the first place.
Means if I use ssid='godhelpmetofindaworkingsolution' it works great but is
not realy what I want.
How does the declaration of table 'session' look? Maybe you've got an
auto_increment playing up.
As told above a "INSERT INTO session (ssid) VALUES ($ssid)" also has the
effect of two new db-entries.
The only way to explain that is that the function is run twice.
Hope you understand and pray to god you know what I do wrong


You're not using session_start(), that's the problem. :-D


--
Sandman[.net]
Sep 14 '05 #5

P: n/a
In article <79**************************@nf1.news-service.com>,
"Johannes A. Brunner" <jo**************@t-link.de> wrote:
Andy Hassall wrote:
The INSERT can't be doing that. The most common mistake in this sort of
thing
is setting up a race condition, so two requests both start a new session
and overlap saving to the database.


But if I have a racecondition it would make no differenc using a variable or
a fixed string.
$result = mysql_query("INSERT INTO session SET
ssid='$season',ip='$rip'",$connect);
$result = mysql_query("INSERT INTO session SET
ssid='season',ip='$rip'",$connect);

But the second line works fine. Just one db-entrie per page hit.
How are you detecting there's no ssid?


@$ssid = $_GET['ssid'];
if (!isset($ssid)) { $ssid = ssid_generate();
ssid_register($ssid,'false','unknown','false'); }

What are you doing with the ssid afterwards?


A lot of stuf,
Here is the way I create the ssid

function ssid_generate() {

$ssid = md5(time());
return $ssid;
}


Whoooo! You either have a VERY low volume site, or lots of surfers with the
same $ssid.

I suggest using microtime() and something more unique to the current page load.
--
Sandman[.net]
Sep 14 '05 #6

P: n/a
Sandman wrote:
I recommend doing a persistent connection to MySQL outside this function. This
way, you're making one new MySQL connection for every time the function is run.
If this is the only function that ever use MySQL, it's no big deal, but if it
isn't.

Lookup mysql_pconnect


mysql_pconnect() could cause a couple of other problems, because it
keeps the connection open *after* the HTTP request has been finished.
See:

http://www.php.net/manual/en/feature...onnections.php

Better use the normal connect and cache the connection for re-use. For
example this way:

function makeDatabaseConnection()
{
static $link;

if (!isset($link))
{
$link = mysql_connect(...);
}
}

With subsequent mysql calls you don't even have to know the connection
because the mysql functions will automatically use the last established
connection.

Nils

Sep 14 '05 #7

P: n/a
On 14 Sep 2005 06:14:12 -0700, "Nils Bandener" <ni*******@applion.net> wrote:
Sandman wrote:
I recommend doing a persistent connection to MySQL outside this function. This
way, you're making one new MySQL connection for every time the function is run.
If this is the only function that ever use MySQL, it's no big deal, but if it
isn't.

Lookup mysql_pconnect


mysql_pconnect() could cause a couple of other problems, because it
keeps the connection open *after* the HTTP request has been finished.
See:

http://www.php.net/manual/en/feature...onnections.php

Better use the normal connect and cache the connection for re-use. For
example this way:

function makeDatabaseConnection()
{
static $link;

if (!isset($link))
{
$link = mysql_connect(...);
}
}

With subsequent mysql calls you don't even have to know the connection
because the mysql functions will automatically use the last established
connection.


If you don't call mysql_close(), you're already using only one MySQL
connection throughout the life of the request anyway, no matter how many times
you call mysql_connect.

http://uk.php.net/mysql_connect

" If a second call is made to mysql_connect() with the same arguments, no new
link will be established, but instead, the link identifier of the already
opened link will be returned."

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 14 '05 #8

P: n/a
Johannes,

I'm not sure if you have found the answer or not. One problem that I
was once having was similar to yours. I'm not sure if this helps at all
in your situation, but I figure I'd posting for anyone else that is
having this problem. Some 'INSERT' statements would happen two times.
This was only happening in Mozilla Firefox, however. After many hours
of searching and playing with my page, I realized that Firefox will
load a page twice (Not sure why) when it contains html or javascript
errors (cant remember which it is now).
One way I kept this double insert from happening (other then fixing the
errors) was to set a session variable on the form page and then check
if it the session variable was still set before the query was executed
and once it was executed I would clear the session variable, keeping it
from doing the double inserts. Hope this helps you or anyone else
having the problem that I was having.

d. spohn

Sep 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.