Avatar of v0rtex79
v0rtex79
 asked on

1062 - Duplicate entry '1' for key 'PRIMARY'

Everything has been working perfectly for years, then decided to upgrade PHP.
Prior PHP v. 5.2.28
Upgraded to PHP v. 5.5 - site completely broken
Downgraded to PHP v. 5.3.29 (unable to downgrade all the way back to 5.2.28)

The error we are getting now is:
1062 - Duplicate entry '1' for key 'PRIMARY'

INSERT INTO products (products_id, products_quantity, products_model, products_image, products_price, products_date_added, products_last_modified, products_date_available, products_weight, products_status, products_tax_class_id, manufacturers_id) VALUES ('1', 100000, 'fonts: 
line 1: Abbey.ttf
font color: Silver
decal type: Windshield Lettering
decal option: 3 inch x 33 inch
', '', '10.95', '2015-03-27 7:52:42', '2015-03-27 7:52:42', '', '0.8', '0', '1', '0')

[TEP STOP]

Open in new window


Table information is
`products_id` INT( 10 ) NOT NULL AUTO_INCREMENT ;

Open in new window


Last incremental id was 92690. Now it is trying to use id of 1. If I delete id 1, then I get the following error:

Untitled.jpg
Here I believe the code causing the issue:
 // custom designed decal
      case 'add_custom' : if ($HTTP_POST_VARS['msg']) {
	  if($HTTP_POST_VARS['customEdit']=='Edit'){
	  
	  $id = tep_edit_custom_in_cart($HTTP_POST_VARS['productID'], $HTTP_POST_VARS['msg'], $HTTP_POST_VARS['msg1'],$HTTP_POST_VARS['msg2'],$HTTP_POST_VARS['msg3'],$HTTP_POST_VARS['msg4'],$HTTP_POST_VARS['msg5'], $HTTP_POST_VARS['font1'], $HTTP_POST_VARS['font2'], $HTTP_POST_VARS['font3'], $HTTP_POST_VARS['font4'], $HTTP_POST_VARS['font5'], $HTTP_POST_VARS['rgbmix'], $HTTP_POST_VARS['effect'], $HTTP_POST_VARS['shadow_direction'], $HTTP_POST_VARS['rgb_sh'], $HTTP_POST_VARS['rgb_ol'], $HTTP_POST_VARS['decal_type'], $HTTP_POST_VARS['type_option'], $HTTP_POST_VARS['width'], $HTTP_POST_VARS['height']);
  tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters), 'NONSSL'));
  
	  }else{
   $id = tep_add_custom_to_cart($HTTP_POST_VARS['msg'], $HTTP_POST_VARS['msg1'],$HTTP_POST_VARS['msg2'],$HTTP_POST_VARS['msg3'],$HTTP_POST_VARS['msg4'],$HTTP_POST_VARS['msg5'], $HTTP_POST_VARS['font1'], $HTTP_POST_VARS['font2'], $HTTP_POST_VARS['font3'], $HTTP_POST_VARS['font4'], $HTTP_POST_VARS['font5'], $HTTP_POST_VARS['rgbmix'], $HTTP_POST_VARS['effect'], $HTTP_POST_VARS['shadow_direction'], $HTTP_POST_VARS['rgb_sh'], $HTTP_POST_VARS['rgb_ol'], $HTTP_POST_VARS['decal_type'], $HTTP_POST_VARS['type_option'], $HTTP_POST_VARS['width'], $HTTP_POST_VARS['height']);
   $cart->add_cart($id, $HTTP_POST_VARS['id']+$quantity, $HTTP_POST_VARS['id']); // add to cart
  tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters), 'NONSSL'));
  }}
                              break;
      // performed by the 'buy now' button in product listings and review page
      case 'buy_now' :        if (isset($HTTP_GET_VARS['products_id'])) {
                               if (tep_has_product_attributes($HTTP_GET_VARS['products_id'])) {
                                 tep_redirect(tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $HTTP_GET_VARS['products_id']));
                               } else {
                                 $cart->add_cart($HTTP_GET_VARS['products_id'], $cart->get_quantity($HTTP_GET_VARS['products_id'])+1);
                               }
                             }

Open in new window

PHPMySQL ServerSQL

Avatar of undefined
Last Comment
gr8gonzo

8/22/2022 - Mon
Dave Baldwin

For the `products_id` VALUE in the INSERT use NULL or 0.  That will cause the auto-increment value to be used.  

$HTTP_POST_VARS should be changed to $_POST before you move to a newer version of PHP.  $HTTP_POST_VARS is deprecated and is expected to be removed in the future.
http://php.net/manual/en/reserved.variables.post.php
ASKER CERTIFIED SOLUTION
jimyX

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
v0rtex79

ASKER
Can you tell me from the code I posted where I would need to make changes? I'm not a coder so I need some hand holding. :)
Dave Baldwin

While I think @jimyX gave you code for the insert, you will likely need to hire a coder to update all of your code to the newer standards.  I believe this will go way beyond hand-holding.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
jimyX

1062 - Duplicate entry '1' for key 'PRIMARY'

INSERT INTO products (products_id, products_quantity, products_model, products_image, products_price, products_date_added, products_last_modified, products_date_available, products_weight, products_status, products_tax_class_id, manufacturers_id) VALUES (/*'1'*/ NULL, 100000, 'fonts:
line 1: Abbey.ttf
font color: Silver
decal type: Windshield Lettering
decal option: 3 inch x 33 inch
', '', '10.95', '2015-03-27 7:52:42', '2015-03-27 7:52:42', '', '0.8', '0', '1', '0')

[TEP STOP]

Edit:
I agree with Dave.
You might have a lot of such so-many-to-come issues.
v0rtex79

ASKER
Can I hire one of you?
Dave Baldwin

Sorry, not me.  I already have plenty of clients.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

I'm a little concerned about this -- if the script is inserting a constant value into the AUTO_INCREMENT id field, it would have been failing years ago.  It may be that your test data set has changed or there are other changes that are not apparent to us here.  In any case, you're on the right track to hire a professional to help you remediate and upgrade.  There are a lot of things that can go wrong and some of them have dire consequences, so don't take any chances with important data.  Back up everything now.

Many of the E-E experts have a "hire me" button on their profiles.

You might want to seriously consider starting over with net-new code.  PHP has come a long way since this was written, and whoever wrote this did not do you any "favors."  At first glance, it looks like something that is untestable.  Like an old car that breaks down -- it may make more sense to get a new car than try to find all the parts and keep repairing things as they break.  Today it would not cost very much to use an ORM framework like Laravel to rebuild the back-end.  A Laravel expert would be expected to have a high hourly rate and very high hourly output.

Going forward, PHP has a change log that you can refer to when you get ready to do upgrades.  All of the compatibility issues are well-documented.  It's a lot to read, but it's require reading for anyone who depends on PHP.
http://php.net/ChangeLog-5.php

Best of luck with your project, ~Ray
gr8gonzo

That code looks a lot like osCommerce. If it is, are you working with a customization / add-on for it and what version of it are you on?