473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

INSERT into mysql alway make two entries

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
8 6981
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
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
$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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jtw | last post by:
I need to update a statistics table through out the day. I would like to insert the first data sampling of the data, then update the existing record for the rest of the day. The problem is that I...
15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
1
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
1
by: rjames.clarke | last post by:
I am inserting a record in to a mysql database. I'd like to know the record number of that record. I use a index field which is auto-incremented. Is there any way to get a field value returned...
6
by: fpcreator2000 | last post by:
Hello everyone. I'm having problems with a page I've created that is used to insert data into a database. It uploads two files into two distinct folder, and it takes the filenames and inserts...
1
by: JPTS | last post by:
I wrote a script that create a table and insert data but actually it didn't do it despite it said it did. What can i do? Search for a compatible version of python and mysql? I downloaded the latest...
3
by: idorjee | last post by:
Hi, Is there any way I could insert the data from my current database table to a new table in a new database easily without the need to insert them one-by-one? I'd appreciate it.
0
by: pbeeke | last post by:
Does anyone know how I could take a weeks entries in a table and copy and insert the same entries into the same table but with the starting date a week on....
2
by: dgourd | last post by:
I am trying to create a registering script, but everytime I try to insert the data into my database i get an error. $query = "INSERT INTO members (username, pass, email, first_name, last_name)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.