SirChick Posted December 14, 2007 Share Posted December 14, 2007 Is there a way to have an insert with a check rather than doing a select query to check first? I'm trying to cut down the amount of queries i got so was hoping it was possible.. .the idea is to insert only if the data is not already existing in the database.. so i thought perhaps an INSERT can use the WHERE clause to check for uniqueness? This is what i got: $InsetIP = mysql_query("INSERT INTO iplogs (UserID, IP) VALUES ('{$_SESSION['Current_User']}', '$ip')") or die(mysql_error()); Then i wanted to add something like WHERE $ip does not already exist for this UserID. Can it be done in one INSERT or will it require a select query first? Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 14, 2007 Share Posted December 14, 2007 You can use the conditional statements in Mysql to check: http://dev.mysql.com/doc/refman/5.1/en/if-statement.html I think you have to use subqueries to actually compare, but it is faster than a new query. Quote Link to comment 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.