You have to make a transaction for this.
First perform select.
SELECT * FROM following where idUser = $id AND followingID = $followingId
Now if above query selects one or more rows then there already exists a relation and if no row (zero rows) then there is no relation. For both situations take action accordingly.
Make sure to use the query according to PHP and SQL syntax. If ID's are char or varchar then in query these should be surrounded by single or double commas.
Like this:
if ID's are varchar or char then
$query = "SELECT * FROM following where idUser = '".$id."' AND followingID = '". $followingId."'";
and if ID's are int or any other digit values (according to column of table)
$query = "SELECT * FROM following where idUser = ".$id." AND followingID = ". $followingId."";
Also make sure the column names and table name I've used are correct.
Now let's first execute the query
$result = mysql_query($query); // this method works but is not preferred. better if insert query here instead of variable $query
Now let's check how many rows it has selected and take appropriate action.
if(mysql_num_rows($result) > 0){
//do something here or show error because relation already exists
}
else{
//relation already do not exists. so you can insert the record here
}
NOTE: the functions mysql_query and mysql_num_rows are deprecated as of PHP 5.5.0. Because I coded in php long ago, so I remember these right now. Alternate you can find on php website. But these functions still work.
http://php.net/manual/en/function.mysql-query.php and
http://php.net/manual/en/function.mysql-num-rows.php