Jump to content

need to know if input already exist in field


attaboy

Recommended Posts

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%) {

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

}

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.