ovy79ro Posted April 23, 2015 Share Posted April 23, 2015 (edited) Hello, I am working now at a script that insert products from an xml files. As i know, the id's from tables ps_product and ps_product_lang (and others) must correspond (foreign keys). My script will be like this: foreach ............ $sql_1 = "INSERT ON `ps_product` (`id_supplier`, `id_manufacturer`, `id_tax_rules_group`,.........) VALUES ('abc', 'cde', '1',..........); ............................ $sql_2 = "INSERT ON `ps_product_lang` (`id_product`, `id_lang`, `description`,.........) VALUES ('???', '6', 'bla bla vla',..........); ........................................ On $sql_2, when insert on ps_product_lang, in column id_product i must insert same id_product from table ps_product. How can i do that? I know i must (or not) use sintax INSERT using SELECT from, but i don't know how to do that. Thank you. Edited April 23, 2015 by ovy79ro (see edit history) Link to comment Share on other sites More sharing options...
electriz Posted April 23, 2015 Share Posted April 23, 2015 Hi, Check this http://php.net/manual/en/mysqli.insert-id.php Link to comment Share on other sites More sharing options...
ovy79ro Posted April 23, 2015 Author Share Posted April 23, 2015 Thanks, but i don't see how this function can help me. Maybe LAST_INSERT_ID(): $value = "SELECT LAST_INSERT_ID() FROM `ps_product`"; $sql_2 = "INSERT ON `ps_product_lang` (`id_product`, `id_lang`, `description`,.........) VALUES ('$value', '6', 'bla bla vla',..........); I will try this cod. 1 Link to comment Share on other sites More sharing options...
electriz Posted April 23, 2015 Share Posted April 23, 2015 Well, you just put a short piece of code. If you are going to execute those queries using php mysqli, that function will do what you want. But using LAST_INSERT_ID() directly in the query is also quite good idea Link to comment Share on other sites More sharing options...
ovy79ro Posted April 23, 2015 Author Share Posted April 23, 2015 (edited) i try to use code: $value = "SELECT LAST_INSERT_ID() FROM `ps_product`"; $sql_2 = "INSERT INTO `ps_product_lang` (`id_product`, `id_lang`, `description`,.........) VALUES ('$value', '6', 'bla bla vla',..........); but it doesn't work. it work only if i set id_product auto_increment in ps_product_lang table. is something wrong if I set id_product auto_increment in ps_product_lang table? Edited April 23, 2015 by ovy79ro (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now