mehnihma Posted December 27, 2012 Share Posted December 27, 2012 (edited) Hi I am new to forum and php I am trying to add values that are missing in second table in table ps_psroduct supplier some values are missing and I want to add the from ps_product. So I want to list values that are missing and insert them in ps_product_supplier, there is no duplication. Some values from ps_product are same in ps_product_supplier but not all so I want to filter thoese values and insert them correctly. So some values exists in ps_product but not in ps_product_supplier and I want to insert missing rows in table by checking id_product and add missing. What I have for now is that I get all the values printed but I need only missing values that I can insert them in ps_product_supplier How to do that? In output I get: [/font][/color] USPJESNO ID 1 ne postoji u drugoj tablici i trenutno: 0 USPJESNO ID 2 ne postoji u drugoj tablici i trenutno: 1 USPJESNO ID 3 ne postoji u drugoj tablici i trenutno: 2 USPJESNO ID 4 ne postoji u drugoj tablici i trenutno: 3 USPJESNO ID 5 ne postoji u drugoj tablici i trenutno: 4 USPJESNO ID 6 ne postoji u drugoj tablici i trenutno: 5 for all the values, but I need to mach IDs and print only missing and create a row for it in ps_product_supplier with values from ps_product code: [/font][/color][/font][/color] <?php $mysql_db = ""; $mysql_user = ""; $mysql_pwd = ""; $con = mysql_connect("localhost", $mysql_user, $mysql_pwd); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($mysql_db, $con); $countUpdated=0; //popravak kategorija mysql_query("UPDATE ps_product_shop prod_shop INNER JOIN ps_product prod USING(id_product) SET prod_shop.id_category_default = prod.id_category_default WHERE prod_shop.id_product BETWEEN 1 AND 62226"); // sve na kunu mysql_query("UPDATE ps_product_supplier SET id_currency = 3 WHERE id_currency = 0"); //dodavanje poreza mysql_query("UPDATE ps_product SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0"); mysql_query("UPDATE ps_product_shop SET id_tax_rules_group = 1 WHERE id_tax_rules_group = 0"); //$kveri = "SELECT id_product_supplier, id_product, id_product_attribute, id_supplier,product_supplier_reference, product_supplier_price_te, id_currency FROM ps_product"; $kveri = "SELECT id_product,id_supplier,supplier_reference, wholesale_price FROM ps_product"; $ispis = mysql_query($kveri) or die(mysql_error()); while ($row = mysql_fetch_array($ispis)){ $trenutnired = $row['id_product']; $trenutnired1 = 0; $trenutnired2 = $row['id_supplier']; $trenutnired4 = $row['supplier_reference']; $trenutnired5 = $row['wholesale_price']; $trenutnired6 = 3; //echo $trenutnired; //$drugatab = "SELECT * FROM ps_product_supplier WHERE id_product = '$trenutnired'"; $drugatab = "SELECT id_product FROM ps_product_supplier WHERE id_product = '$trenutnired'"; $rezultati = mysql_query($drugatab) or die(mysql_error()); if(mysql_num_rows($rezultati)){ // Successful query... echo "USPJESNO ID $trenutnired ne postoji u drugoj tablici i trenutno: $countUpdated<br />"; //mysql_query("INSERT INTO ps_product_supplier (id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency) VALUES ('', '$trenutnired', '$trenutnired1', '$trenutnired2', '$trenutnired4', '$trenutnired5', '$trenutnired6')"); $countUpdated++; } else { echo "ID $trenutnired ne postoji u drugoj tablici i trenutno: $countUpdated<br />"; } } ?> This query works for one ID to insert data but I need for all that are missing: INSERT INTO ps_product_supplier (id_product_supplier, id_product, id_product_attribute, id_supplier, product_supplier_reference, product_supplier_price_te, id_currency) VALUES(NULL, 6216, 0, (SELECT id_supplier FROM ps_product WHERE id_product = 6216), (SELECT supplier_reference FROM ps_product WHERE id_product = 6216), (SELECT wholesale_price FROM ps_product WHERE id_product = 6216), 3); Would it be easier just to use MySql? Thanks Edited December 27, 2012 by mehnihma Quote Link to comment https://forums.phpfreaks.com/topic/272421-php-insert-row-in-database/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.