Forums / Contribution-Writing Guidelines / Retrieving the last ` LAST_INSERT_ID();` on a SQL Patch

Retrieving the last ` LAST_INSERT_ID();` on a SQL Patch

Views: 1,761

Results 1 to 2 of 2
18 Jan 2024, 4:30 AM
#1
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Retrieving the last ` LAST_INSERT_ID();` on a SQL Patch

This is for PHP 8.2, ZC 1.5.8.

I'm trying to write an auto-installer for a custom product type but I'm having a problem with the product type layout insert. The idea is that I use the last insert ID from product_types and insert that into the product_type_layout table as needed. But everytime I do so using the "Install SQL patch" in the admin area, I get an error saying something akin to:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '@insert_ID

Or something like

FUNCTION zencart.1LAST_INSERT_ID does not exist :: INSERT INTO jg1_product_type_layout (configuration_title, configuration_key, configuration_value, configuration_description, product_type_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('Show Model Number', 'SHOW_PRODUCT_CARDS_FAB_INFO_MODEL', '0', 'Display Model Number on Product Info 0= off 1= on', LAST_INSERT_ID(), 1, NOW(), NOW(), NULL, 'zen_cfg_select_drop_down(array(array(\'id\'=>\'1\', \'text\'=>\'True\'),

Here is the SQL code I'm trying to use:

INSERT INTO `product_types`     (`type_name`, `type_handler`, `type_master_type`, `allow_add_to_cart`, `default_image`, `date_added`, `last_modified`) 
    VALUES 
    ('Cards - Flesh and Blood', 'product_cards_fab', '1', 'Y', '', NOW(), NOW());


INSERT INTO `product_type_layout` (`configuration_title`, `configuration_key`, `configuration_value`, `configuration_description`, `product_type_id`, `sort_order`, `last_modified`, `date_added`, `use_function`, `set_function`) VALUES ('Show Model Number', 'SHOW_PRODUCT_CARDS_FAB_INFO_MODEL', '0', 'Display Model Number on Product Info 0= off 1= on', LAST_INSERT_ID(), 1, NOW(), NOW(), NULL, 'zen_cfg_select_drop_down(array(array(\'id\'=>\'1\', \'text\'=>\'True\'), array(\'id\'=>\'0\', \'text\'=>\'False\')), '),

Should I just use a $db->Execute instead or run something like this directly in PHPMyAdmin?

18 Jan 2024, 8:19 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Retrieving the last ` LAST_INSERT_ID();` on a SQL Patch

I just ran this in SQL Patch without problem (only difference from yours was changed the last comma to a semicolon):

INSERT INTO `product_types`     (`type_name`, `type_handler`, `type_master_type`, `allow_add_to_cart`, `default_image`, `date_added`, `last_modified`) 
VALUES ('Cards - Flesh and Blood', 'product_cards_fab', '1', 'Y', '', NOW(), NOW());
INSERT INTO `product_type_layout` (`configuration_title`, `configuration_key`, `configuration_value`, `configuration_description`, `product_type_id`, `sort_order`, `last_modified`, `date_added`, `use_function`, `set_function`) VALUES ('Show Model Number', 'SHOW_PRODUCT_CARDS_FAB_INFO_MODEL', '0', 'Display Model Number on Product Info 0= off 1= on', LAST_INSERT_ID(), 1, NOW(), NOW(), NULL, 'zen_cfg_select_drop_down(array(array(\'id\'=>\'1\', \'text\'=>\'True\'), array(\'id\'=>\'0\', \'text\'=>\'False\')), ');

You could optionally prefix it with something like the following on its own line before the first INSERT, but I didn't need to do that for it to work on my site.

#NEXT_X_ROWS_AS_ONE_COMMAND:3

For reference, I'm using MariaDB 11.2.2 on this server, but this isn't anything unique to MariaDB vs MySQL, and it's not using anything "new" so should be fine to much older versions of MySQL and MariaDB.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.