Jump to content

INSERT INTO doing double duty


devknob

Recommended Posts

PHP version  5.1.6

MySQL version 5.0.27-standard-log

 

Table is super simple, 1 primary key, uid, unixtime(timestamp),blog, pro, bull, pix

 

if($uid > 0 && $id != $uid){

include('connection.php');

mysql_query("INSERT INTO history SET uid='$uid', pro='$id'"); }

 

Thats all. Does anyone know why this would create two identical records every time?

Link to comment
Share on other sites

I am having the same problem and I can not figure out why. I didn't see this post, and created one called "Saving Data Twice."

 

I've placed an exit after the save and even cleared the post variables, and it still happens. I believe it is not doubling the declared insert, but rather duplicating the current record in the database. I believe this because, while testing, for a while, rather than saving the same data twice, it was duplicating another current record in the database. I'm sorry I can't give you any more detail than that - I don't know what may have moved the pointer, but it may be an eye opener.

 

That said, and still being uncertain, I need to ask if you are posting to your form file or a different file?

Link to comment
Share on other sites

My guess it that the script is running twice...

 

That, of course, was my first thought, especially since the file is posting to itself. However, I have tried different things; clear($_POST); header("location: otherfile.php"); exit and finally separated the HTML and PHP into to different files and posted to the PHP file. All these had the same result. I also tried halting the file and waiting for user input directly after the SQL insert, and then physically checking the database before proceeding.  I have taken the code down to the bare bones and only 4 fields and still the same result.

 

I think the problem has to be in the database setup, although it is quite simple:

15 fields; integers; tinyintegers; varchar; date; char & float

1 auto-increment id field as primary

two indexes (first & last name)

 

Link to comment
Share on other sites

My guess it that the script is running twice...

 

If I replace the INSERT INTO, with an echo "test"; it only prints test to the screen once. Also if I put the insert into command directly into mysql, it only inserts one time. Its whacky because I have copy and pasted the script into other php files and it only inserts once.

 

ALSO: I am posting to SELF (same file)

 

If noone has a fix for this im assuming its just an error with php. I guess.

Link to comment
Share on other sites

Does anyone have a suggestion to work around? I suppose I can insert an entry, then delete the last entry... but thats lame, and would probably have to be fixed once an update happens. All my other INSERT INTO commands work as needed. lame... ???

Link to comment
Share on other sites

devknob -

I spent hours working on this, including a lot of time searching the web for other people with the same problem, or some MySQL guru who might have a clue, and coming up empty handed. I've been told things like "well, if you validate a unique field, you won't have that problem".  True, but I want to find out why the problem is occurring - not hide it and pretend it didn't happen.

 

Currently, the problem is gone.

 

All I can tell you is that I added two fields to the database, turned off auto-increment on my "id" field, moved the primary key to another field (and forgot to remove the primary key from the auto-increment field), saved two records, and there were no duplicate saves.  I then turned auto-increment back on in the id field, removed the primary key from the second field, and all is working fine.

 

At this point, I would say the problem really appears to be a MySQL bug.  If you read my other post ("Saving Data Twice"), you would remember that I have two identical databases (except they are different MySQL versions). I'm swamped with work, and have to go out of town, but next week I will go to the other database, and do a lot more careful investigation. When I am done, I will post my findings here.

Link to comment
Share on other sites

use second query after the insert statement

 

$result1=mysql_query("INSERT INTO history SET uid='$uid', pro='$id'");

$result2=mysql_query("OPTIMIZE TABLE table_name");

echo mysql_affected_rows($result1);

 

 

just to make sure that ur insert statement not executing two times.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.