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? Link to comment https://forums.phpfreaks.com/topic/81724-insert-with-a-check/ 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. Link to comment https://forums.phpfreaks.com/topic/81724-insert-with-a-check/#findComment-415093 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.