attaboy Posted March 18, 2012 Share Posted March 18, 2012 I have a field in a table named flights I hope to find a 1 line statement to bypass inserting a new row if $flight_no is already in the field flight_no btw flight_no is the primary key and won't allow a duplicate anyway but I want to avoid the error message this is what I tried but doesn't work if(SELECT * FROM flights WHERE flight_no !LIKE %$flight_no%) { Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/ Share on other sites More sharing options...
EmperorJazzy Posted March 18, 2012 Share Posted March 18, 2012 Might I suggest; $query = "SELECT FLIGHT_NO FROM FLIGHTS WHERE FLIGHT_NO = " . $flightno; Call the function for the query results If recordcount > 0 Then insert else dont insert It's not ONE LINE, but you could combine them together, however, it wouldn't be prudent to combine into one line as it'll make it harder to debug. Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/#findComment-1328656 Share on other sites More sharing options...
SaCH Posted March 18, 2012 Share Posted March 18, 2012 Code will be this $query = "SELECT `flights_no` FROM `flights` WHERE `flights_no` = '$flights_no'"; $result = mysql_num_rows(mysql_query($query)); if($result == "0") { //Value is not existing do something.. } else { //value already existing, do something } Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/#findComment-1328662 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2012 Share Posted March 18, 2012 I think I'd just run the INSERT query, check for an error that is not a duplicate key error, and handle it in the logic of the code if there is one. $query = "INSERT INTO table ( flight_no, etcetera ) VALUES ( $flight_no, $etcetera )"; if( !$result = mysql_query( $query ) ) { if( mysql_error_no() !== 1061 ) { // There was a db error other than a duplicate key, so handle it accordingly } else { // query executed } Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/#findComment-1328665 Share on other sites More sharing options...
attaboy Posted March 18, 2012 Author Share Posted March 18, 2012 I think I like this last one best. So many people really know there s#$t here, I love this place! Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/#findComment-1328668 Share on other sites More sharing options...
attaboy Posted March 18, 2012 Author Share Posted March 18, 2012 for anyone reading the last example is the best but he had 2 typos it should be mysql_errno() !== 1062 Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/#findComment-1328689 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2012 Share Posted March 18, 2012 Yeah, I guess I did. Glad you figured it out and got it working, though. Quote Link to comment https://forums.phpfreaks.com/topic/259181-need-to-know-if-input-already-exist-in-field/#findComment-1328763 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.