My users have the ability to connect with each other, or to connect with the other profile & all connections of that profile at the same time.
But it might be, that the user is already connected with one of the connections from the other user. I need to make sure there won't be two identical rows in the database:
my query looks like this:
if (isset($_POST['crewconnect'])) {
include '../php/dbconnect.php';
$connections = $DBcon->query("SELECT connection_id FROM tbl_current_userconnections WHERE user_id=$profileID");
while($row = $connections->fetch_array()){
$crewmemberID = $row['connection_id'];
$DBcon->query("DELETE FROM tbl_former_userconnections WHERE user_id=$activeID AND connection_id=$crewmemberID");
$DBcon->query("DELETE FROM tbl_former_userconnections WHERE user_id=$crewmemberID AND connection_id=$activeID");
//the following query needs to be changed:
$DBcon->query("INSERT INTO tbl_current_userconnections(user_id, connection_id) VALUES('$activeID','$crewmemberID'),('$crewmemberID','$activeID')");
}
$DBcon->query("INSERT INTO tbl_current_userconnections(user_id, connection_id) VALUES('$activeID','$profileID'),('$profileID','$activeID')");
$DBcon->close();
}
I thought about something like this for the part to be changed:
$DBcon->query("IF (NOT EXISTS(SELECT user_id, connection_id FROM tbl_current_userconnections WHERE user_id=$activeID AND connection_id=$crewmemberID)) INSERT INTO tbl_current_userconnections(user_id, connection_id) VALUES('$activeID','$crewmemberID'),('$crewmemberID','$activeID')");
but this is not working, so far...