Destramic Posted May 15, 2015 Share Posted May 15, 2015 hey guys im wondering if it's possible to insert a row if a value exists inside another table? for instance the code below i wish to make a log in attempt record if the user actually exists in the users table INSERT INTO login_attempts ('identity') VALUES ('name@example.com') SELECT user_id FROM users WHERE email_address = 'name@example.com' thanks you Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/ Share on other sites More sharing options...
Barand Posted May 15, 2015 Share Posted May 15, 2015 (edited) You are mixing statement syntaxes here. What do want to insert into the identity column? If it is the id of the user then INSERT INTO login_attempts ('identity') SELECT user_id FROM users WHERE email_address = 'name@example.com' Edited May 15, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/#findComment-1512002 Share on other sites More sharing options...
Destramic Posted May 15, 2015 Author Share Posted May 15, 2015 well id like to check if users exists first before entering a log in attempt...after a bit more researching i think i need something like this but it doesn't work INSERT INTO login_attempts (identity) VALUES ('destramic@hotmail.com') WHERE NOT EXISTS (SELECT user_id FROM users WHERE email_address = 'destramic@hotmail.com') is this even possible? thank you Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/#findComment-1512007 Share on other sites More sharing options...
wavix Posted May 15, 2015 Share Posted May 15, 2015 Use INNER JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/#findComment-1512009 Share on other sites More sharing options...
Barand Posted May 15, 2015 Share Posted May 15, 2015 INSERT INTO login_attempts ('identity') SELECT email_address FROM users WHERE email_address = 'name@example.com' If email doesn't exist in users table then no insert. Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/#findComment-1512012 Share on other sites More sharing options...
Destramic Posted May 16, 2015 Author Share Posted May 16, 2015 INSERT INTO login_attempts ('identity') SELECT email_address FROM users WHERE email_address = 'name@example.com' If email doesn't exist in users table then no insert. brilliant thank you...is it also possible to add custom values to this query other than what is selected from the users table? Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/#findComment-1512024 Share on other sites More sharing options...
Barand Posted May 16, 2015 Share Posted May 16, 2015 Sure, you can select constant values just as in any select statement INSERT INTO login_attempts ('identity', colA, colB, colC) SELECT email_address, 123, 'ABC', NOW() FROM users WHERE email_address = 'name@example.com' Quote Link to comment https://forums.phpfreaks.com/topic/296345-insert-if-user-exists/#findComment-1512048 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.