473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,$ui d,$admin){

$connect = mysql_connect( $GLOBALS["db_host"],
$GLOBALS["db_user"],
$GLOBALS["db_passwor d"])
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("IN SERT INTO session SET ssid='$season', ip='$rip'",$con nect);
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='godhelpme tofindaworkings olution' 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 7002
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,$ui d,$admin){

$connect = mysql_connect( $GLOBALS["db_host"],
$GLOBALS["db_user"],
$GLOBALS["db_passwor d"])
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("IN SERT INTO session SET ssid='$season', ip='$rip'",$con nect);
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='godhelpme tofindaworkings olution' 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.u k :: 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("IN SERT INTO session SET ssid='$season', ip='$rip'",$con nect);
$result = mysql_query("IN SERT INTO session SET ssid='season',i p='$rip'",$conn ect);

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','u nknown','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("IN SERT INTO session SET ssid='$season', ip='$rip'",$con nect);


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,$ui d,$admin){

$connect = mysql_connect( $GLOBALS["db_host"],
$GLOBALS["db_user"],
$GLOBALS["db_passwor d"])
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.sessio ns" below to insert into the correct database/table
$result = mysql_query("IN SERT INTO session SET
ssid='$season', ip='$rip'",$con nect);
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.sessio ns (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='godhelpme tofindaworkings olution' 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("IN SERT INTO session SET
ssid='$season', ip='$rip'",$con nect);
$result = mysql_query("IN SERT INTO session SET
ssid='season',i p='$rip'",$conn ect);

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','u nknown','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 makeDatabaseCon nection()
{
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*******@appl ion.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 makeDatabaseCon nection()
{
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.u k :: 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
16422
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 do not alway know when the first sampling will start. Keys for the table are username and date. So each user will have only one entry per day. I have been checking to see if the record exists - select .. where username=... AND date =.... ...
15
7398
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 atmosphere. Good food.", " (Harry Houdini - 03/01/2004)"); INSERT INTO `reviews` VALUES("", "Le Chow Place", "Lunch", "yada yada", " (Herbert Hoover - 03/03/2004)"); INSERT INTO `reviews` VALUES("", "Golden Dragon", "Lunch", "Exquisite.
1
838
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 to completion. Version 4 seems to STOP when it encounters a duplicate entry, so that the records before the duplicate are inserted and the records after the duplicate are not inserted. 3.22.27.1 - previous ver MySQL that did not return error...
1
13378
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 after insertion? I have been just running another query for the last record, but if someone does insertion a split second after the first insertion the last record would not be the one of interest.
6
2179
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 them into a MYSQL database along with other product information. Here is the entire .php file. I list it because the errors are not showing at all, and I need a fresh pair of eyes to look at it. Any answers, critisims (constructive), or other talk...
1
1529
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 version of both
3
1677
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
878
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. class_id class_dte class_start_time class_end_time class_name class_def_id 6 21/11/2007 13:25 19/11/2007 11:00 19/11/2007 12:00 Spanish 1 8 21/11/2007 14:44 19/11/2007 12:00 19/11/2007 13:00 French 1 9 21/11/2007 14:48 19/11/2007 16:30 19/11/2007 17:30 Maths 3 10 21/11/2007...
2
1639
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) VALUES ($username, $password, $email, $f_name , $l_name);"; $q = mysqli_real_query($dbc, $query); if($q) { // My code goes here } else { // Display an error }
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.