查找sql​​表中是否存在传入数据

I have a form where the user is supposed to input data (name, streetname, streetnumber) and then i want to check if that data exists in the table (name, streetname+streetnumber) so i can use that row instead of making a new one. Is there an easy way to do this or will i have to fetch the data into an array and search that somehow?

I have this code:

$sql = "INSERT INTO 725G54_proj_address VALUES           (NULL,'$streetname','$streetnumber')";
$result1 = mysql_query($sql,$con);
$address=mysql_insert_id();
echo "ID of last inserted record is: " . $address;
echo "<br>";


$sql = "INSERT INTO 725G54_proj_contact VALUES ('Null','$name','$telephonenumber')";
$result2 = mysql_query($sql,$con);
$contact = mysql_insert_id();
echo "ID of last inserted record is: " . $contact;


$sql = "INSERT INTO 725G54_proj_LivesAt VALUES ('$contact','$address')";
$result3 = mysql_query($sql,$con);`enter code here`

What i would like to is is find out if example Mainstreet 31 allready exists in proj_address and if it does, use its autoincremented id for the insert into proj_Lives_at.

Edit: I decided to go with sending queries to check if the data existed. I load that into a variable and check if it is empty, but there is some problem so that "if(empty)" doesn't work as it should. the other way around ("if(!empty)") works however.

the query and loading look like this:

$sql2="SELECT 725G54_proj_address.Address_ID, GROUP_CONCAT( cast( concat( 725G54_proj_address.Street_Name, ' ', 725G54_proj_address.Street_Number ) AS char ) SEPARATOR ', ') AS addresses
    FROM 725G54_proj_address
    WHERE 725G54_proj_address.Street_Name = '$streetname' AND 725G54_proj_address.Street_Number= '$streetnumber'
    GROUP BY 725G54_proj_address.Address_ID
    ORDER BY 725G54_proj_address.Address_ID ASC";
$result2=mysql_query($sql2);

Edit 2: I have tried switching to using count() instead of empty() and it found the variable as populated even when it shouldnt.

It sounds like you are creating a many-to-one relationship (potentially) and should therefore be storing that data in a separate lookup table. What I suggest in that case is to first try to insert that row using an "INSERT IGNORE" statement (http://dev.mysql.com/doc/refman/5.5/en/insert.html) and then use mysql_affected_rows() to see if it made a change. If it didn't then you know there's a duplicate row, which you can select, take the ID of, and then you're done.

As somebody mentioned in one of the comments above, you can make that into a stored process.

Assuming you've created proper keys on your table (i.e., a unique key across (name, streetname, streetnumber), or something similar), then MySQL INSERT...ON DUPLICATE KEY UPDATE, or possibly REPLACE INTO, might be useful here. The former will convert your INSERT statement into an UPDATE if the key to be inserted already exists in the table, and the latter will act as regular INSERT unless the key already exists, in which case it will first DELETE the existing row before INSERTing the new data.

Of course, it's not exactly an onerous burden simply to SELECT COUNT(*) AS n_existing FROM table WHERE..., and then inspect n_existing in the result set to see whether a row already exists with the same key you're preparing to INSERT, and there is an argument to be made from the principle of least astonishment that it would be better to SELECT before INSERTing than to use abstruse SQL statements which may not have been encountered before by anyone else who's going to be maintaining your code. Still, the option exists, should you prefer it.