Forum Moderators: coopster

Message Too Old, No Replies

MySQL insert query inserting duplicate records

         

kunwarbs

1:11 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



I am running a MySQL insert query and surprisingly it is inserting two duplicate records into the table.

I tried to print the query on the screen to see if the query was running twice but the query shows up only once on the screen when I echo the query.

I don't have any page refresh script either on the page.

Any suggestions?

SteveWh

2:44 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



I'm not at all certain that I'll be able to help with this, but I'm sure it will help others to help you if you provide the relevant portion of the query code, and any of the surrounding code that might be relevant to the situation.

jecasc

3:24 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess there is something wrong with your code. But since you did not post ist, I can't tell you what.

siMKin

7:07 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



place an exit or die() just after the query and see if it is still inserted twice. If not, and an echo just after it is not executed twice then clearly another part of your code is executing a similar query

Matthew1980

7:18 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kunwarbs,

If you post the code that delivers this sql to your server we can better assist you, other than that it's going to be a bit difficult to assess what the cause/fix is.

Cheers,
MRb

fissssssi

2:35 am on Jan 19, 2011 (gmt 0)

10+ Year Member



I'm having the same problem ... my code is below, any help very very gratefully received!

Thanks,


Adam

-----------------------
$actRef=$_POST['bkRef'];
$actTitle=$_POST['bkTitle'];
$actPrice=$_POST['bkPrice'];
$actType=$_POST['bkType'];

$fctRef =mysql_real_escape_string($actRef);
$fctTitle =mysql_real_escape_string($actTitle);
$fctPrice =mysql_real_escape_string($actPrice);
$fctType =mysql_real_escape_string($actType);

mysql_query("INSERT INTO `mytablename` (prdID, prdTitle, prdPrice) VALUES ('$fctRef', '$fctTitle', '$fctPrice')") or die(mysql_error());

coopster

7:11 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is the error? Duplicate record? If so, check your unique/primary key on the table and dump the values of your table columns along with the mysql_error to see why. You are using user-supplied input to do the INSERT so you are going to have to figure out how to handle what might be duplicate entries.

Demaestro

10:33 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does it select from another table to do the insert?

Something like this?

insert into table (value1, value2) select value1, value2 from table2

If so you may want to limit the select using limit or adding a where clause that ensures that only one row will be returned.

The only other reason a single insert statement would insert more than one row is if it was in a loop.

Again all we can do is guess without a code snippet.

Matthew1980

1:22 pm on Jan 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there fissssssi,

Welcome to WebmasterWorld!

Ideally this could have been a new thread, but that's a mods/admin decision.

As for your code, this looks fine, though I have tidied it up a wee bit:

$fctRef = mysql_real_escape_string($_POST['bkRef']);
$fctTitle = mysql_real_escape_string($_POST['bkTitle']);
$fctPrice = mysql_real_escape_string($_POST['bkPrice']);
$fctType = mysql_real_escape_string($_POST['bkType']);

$sql_query = "INSERT INTO `mytablename` (`prdID`, `prdTitle`, `prdPrice`) VALUES ('".$fctRef."', '".$fctTitle."', '".$fctPrice."') ";

mysql_query($sql_query) or die(mysql_error());

There are other things that you can do but for now are you sure that you have a db connection going, and because your not in a loop there shouldn't be duplicate content. Unless this script is being called twice :)

I popped the sql outside the function so that you can echo it to screen so that you can debug it to screen so that you can see that the values are being populated as expected.

Not sure why you were assigning the $_POST twice either, takes up more memory, try to be as streamlined as you can; nest functions where you can, makes the applications run better, but where you do need things structured for debugging that's always better as it makes for better coding standards IMO.

Cheers,
MRb